Reputation: 152
I am facing an issue in a gridview in ASP.NET when I dynamically add a new row to a pre-populated GridView
My Grid View consists of element of two types ( say A and B) which are pre-populated from the database
The code I have written first calculates the count of elements of Type A , then adds a new row as a "ROW Seperator" , between the elements of Row A and Row B. I write this bit of logic in the row data bound event of the grid view
My grid view consists of checkboxes for both elements of type A and Type B.
Now if I check the last element in the grid View , I realizied after putting a break point that the given element no longer exists in the gridView although it visually appears in the front end for the user to "check".
I belive this is something to do with the dynamic row separator I added to the gridView but I can't seem to understand why.
Below is my code.
//Page Load
protected void Page_Load(object sender, EventArgs e)
{
//Getting count of elements of type A and storing it in a global variable
globalA_count= GetCount(dataset);
gridView.DataBind();
}
// GRIDVIEW DATABOUND
protected void gvAccessories_RowDataBound(object sender, GridViewRowEventArgs e)
{
//DATA SET IS ALREADY SORTED SO THAT "A" ELEMNTS AND 'B" ELEMENTS ARE GROUPED TOGETHERE
if (e.Row.Cells[4].Text == "B")
{
e.Row.BackColor = System.Drawing.Color.FromArgb(236, 236, 236);
}
/*** "localA_Count" is used as a counter to count "A" elements within rowdatabound
* globalA_count" holds the count of "A" elements already calculated
* when "localA_Count" equals "globalA_count",it means all
A type elements are already traversed**/
else if (e.Row.Cells[4].Text == "A")
{
localA_Count++;
if (localA_Count== globalA_count)
{
// ADD ROW SEPERATOR
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
row.CssClass = "StdGridHeader";
TableCell cell1 = new TableCell();
cell1.Text = "sample text"
cell1.HorizontalAlign = HorizontalAlign.Center;
cell1.ColumnSpan = 4;
row.Cells.Add(cell1);
((GridView)sender).Controls[0].Controls.AddAt(globalA_count+ 1, row);
}
}
}
Upvotes: 0
Views: 242
Reputation: 145
Are you adding code to correct event? Your gridview id is "gridview" but event is different gvAccessories_RowDataBound() ?
Upvotes: 0