[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to add Row to datagrid OnItemDataBoundEventHandler

Posted on 2004-09-21
7
Medium Priority
?
258 Views
Last Modified: 2008-02-01
How do I add a row and then columns to a datagrid?  Let me know if you need more info.  Also, if you can show me how to NOT display the ID column, please do because I have to do a second access without the ID, which is slow.
Here is the existing code:

// access data with ID column
recordX = dbAccess.getDataTable(con, table, sql);
// add second access to data without ID column for binding
DataTable data = dbAccess.getDataTable(con, table, sqlSv);
dg.DataSource = data;
dg.DataBind();


protected void OnItemDataBoundEventHandler(Object sender, DataGridItemEventArgs e)
{
      ListItemType type = e.Item.ItemType;
      int priceSBT = 0;
      string svDate = "";

      if (type == ListItemType.Header ||
          type == ListItemType.Footer ||
          type == ListItemType.Separator) return;
          try
         {  
              DataRow dr = ((DataRowView)e.Item.DataItem).Row;
              int iCtr = e.Item.Controls.Count;
              int xCtr = 0;
              string table = Session["tablename"].ToString();
              xCtr = iCtr-1;
 
// for row (or y) - must take into account which page it is (dg.CurrentPageIndex) * no. of rows on page (15)
                int y = dg.Items.Count+(dg.CurrentPageIndex*15);
                        
// Add Subtotals on Date Change
                if (recordX.Rows[y]["Order Dt"].ToString() == svDate.ToString())
                {
                  string sContent = recordX.Rows[y]["Price"].ToString();
                  int iContent = Convert.ToInt32(sContent);
                  priceSBT += iContent;
                 }
                 else
                 {
     //**************** add row then add column with subtotal priceSBT HERE

                                       svDate = recordX.Rows[y]["Order Dt"].ToString();
                            }
                     }
 }
0
Comment
Question by:dcass
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
7 Comments
 
LVL 5

Expert Comment

by:tomasX2
ID: 12118307
What is it exactly that your trying to do?
0
 

Author Comment

by:dcass
ID: 12118903
Add a row with a column for the subtotal of several lines on a date break.
0
 
LVL 3

Accepted Solution

by:
w_shaila earned 1500 total points
ID: 12119071
1) Caleculate subtotal with the columns in datatable by looping datatable rows
2) Create a Row and assign subtotal to this row
3) bind this datatable to dataGrid()

Regarding  ID column, you can add the colums what ever you want to the datagrid manually. But that ID column should be there in the datasource.you can manage with DataKeyField  without showing in grid as a clomn.

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:dcass
ID: 12119091
Can you show me the code it would take to do these two things?
I am new and don't know how.
0
 

Author Comment

by:dcass
ID: 12119819
Unfortunately my boss expects this in the morning to show his boss -
Can anyone help me on this?
I've tried:

recordX = dbAccess.getDataTable(con, table, sql);
DataTable data = dbAccess.getDataTable(con, table, sqlSv);

string svDt = "";
string cssClass = "";
string tdclass = "";
foreach(DataRow row in data.Rows)
{
   string Price=row["Price"].ToString();
   string ordDt=row["Order Dt"].ToString();
    if(ordDt!=svDt)
    {
                svDt = ordDt;
                DataColumn col = new DataColumn();
      DataRow rowx;
      col.DataType = System.Type.GetType("System.String");
      col.ColumnName = "PriceSBT";
      data.Columns.Add(col);
      rowx = data.NewRow();
      rowx["PriceSBT"] = Price;
      data.Rows.Add(rowx);
      }
}

But I get an error:
Collection was modified; enumeration operation may not execute.

Any other way to do this?



                  dg.DataSource = data;
0
 

Author Comment

by:dcass
ID: 12120133
Never mind - I finally got it - sort of.
I gathered you can't add rows for subtotals because it adds them to the bottom of the table instead of where you need it - on the date breaks, so I added a column instead that I populated on date change.
If there is a way to add rows in the middle of a data table (other than rewrite the program and add it to the database table), please let me know.
Otherwise, please close and refund points.
0
 

Author Comment

by:dcass
ID: 12142832
I decided to give point for responding -
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question