dcass
asked on
How to add Row to datagrid OnItemDataBoundEventHandler
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 OnItemDataBoundEventHandle r(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.DataI tem).Row;
int iCtr = e.Item.Controls.Count;
int xCtr = 0;
string table = Session["tablename"].ToStr ing();
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.Current PageIndex* 15);
// Add Subtotals on Date Change
if (recordX.Rows[y]["Order Dt"].ToString() == svDate.ToString())
{
string sContent = recordX.Rows[y]["Price"].T oString();
int iContent = Convert.ToInt32(sContent);
priceSBT += iContent;
}
else
{
//**************** add row then add column with subtotal priceSBT HERE
svDate = recordX.Rows[y]["Order Dt"].ToString();
}
}
}
Here is the existing code:
// access data with ID column
recordX = dbAccess.getDataTable(con,
// add second access to data without ID column for binding
DataTable data = dbAccess.getDataTable(con,
dg.DataSource = data;
dg.DataBind();
protected void OnItemDataBoundEventHandle
{
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.DataI
int iCtr = e.Item.Controls.Count;
int xCtr = 0;
string table = Session["tablename"].ToStr
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.Current
// Add Subtotals on Date Change
if (recordX.Rows[y]["Order Dt"].ToString() == svDate.ToString())
{
string sContent = recordX.Rows[y]["Price"].T
int iContent = Convert.ToInt32(sContent);
priceSBT += iContent;
}
else
{
//**************** add row then add column with subtotal priceSBT HERE
svDate = recordX.Rows[y]["Order Dt"].ToString();
}
}
}
What is it exactly that your trying to do?
ASKER
Add a row with a column for the subtotal of several lines on a date break.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Can you show me the code it would take to do these two things?
I am new and don't know how.
I am new and don't know how.
ASKER
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"].ToStrin g();
string ordDt=row["Order Dt"].ToString();
if(ordDt!=svDt)
{
svDt = ordDt;
DataColumn col = new DataColumn();
DataRow rowx;
col.DataType = System.Type.GetType("Syste m.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;
Can anyone help me on this?
I've tried:
recordX = dbAccess.getDataTable(con,
DataTable data = dbAccess.getDataTable(con,
string svDt = "";
string cssClass = "";
string tdclass = "";
foreach(DataRow row in data.Rows)
{
string Price=row["Price"].ToStrin
string ordDt=row["Order Dt"].ToString();
if(ordDt!=svDt)
{
svDt = ordDt;
DataColumn col = new DataColumn();
DataRow rowx;
col.DataType = System.Type.GetType("Syste
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;
ASKER
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.
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.
ASKER
I decided to give point for responding -