Click here to Skip to main content
15,611,156 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a gridview, which on the row data bound method that inserts a row when comparing values on a particular field. If the two values are different, a new row is inserted, if not no row is inserted. This coding is working as a grouping gridview, and is working correctly. However, I have problem, and problem is counting the rows between each group, which I want to display in my blank row between each group, the value of which I am holding in a hiddenfield. When I try to do it, the first group doesn't show the result, but is displayed on the next group. I am pretty sure that I am doing something wrong. My approach is that if the value between the last row and the existing row are different, then the value is one, on the next row data bound event, if the values are the same then the value is increased by one, else the value is one because it is a new group.

Here is my code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  Table tbl = e.Row.Parent as Table;
  TableCell cell = new TableCell();

  CheckBox chk = new CheckBox();
  Label lbl2 = new Label();

  HiddenField rowValue = new HiddenField();

  if (e.Row.RowType == DataControlRowType.Header)
    e.Row.Visible = false;
    if (e.Row.RowType == DataControlRowType.DataRow)
      Session["count"] = 1;

      Label lbl = (Label)e.Row.FindControl("lblValue");

      string str1 = ((Label)(lbl)).Text;
      string prevStr = (string)Session["previousStr"];

      if (prevStr == str1)
        lbl.Visible = false;
        lbl.Text = string.Empty;

        counter += 1;
        Session["count"] = (int)Session["count"] + counter;

        prevStr = sr1;
        Session["previousStr"] = prevStr;

        if (tbl != null)
          GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);

          rowValue.ID = "hdnNumRows";
          rowValue.Value = Convert.ToString(Session["count"]);

          lbl2.Text = Convert.ToString(Session["count"]);

          cell.ColumnSpan = this.GridView1.Columns.Count;
          cell.Width = Unit.Percentage(100);
          cell.BackColor = System.Drawing.Color.Aqua;

          HtmlGenericControl span = new HtmlGenericControl("span");
          span.InnerHtml = prevStr;

          tbl.Rows.AddAt(tbl.Rows.Count - 1, row);
Updated 26-Mar-10 9:31am

1 solution

Several things wrong with this approach that I see.

One, RowDataBound is not the event you should be using. You should not be adding to the grid from it. Try RowCreated instead.

Two, I believe grouping the datasource itself before binding to the grid would be a better approach, making most the code in the RowDataBound or RowCreated handlers much simplier.

Three, Session["count"] is pretty much useless. You are trying to use it as a static field and this is not how or what session state is used for. Again, filtering the the datasource and including a number there I believe would be a better use and require less code to format the grid view.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900