Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void GV_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
        Label lblMaterial = (Label)e.Row.FindControl("lblMaterial");
        Label lblMaterialValue = (Label)e.Row.FindControl("LblMaterialValue");
        Label lblMaterialQty = (Label)e.Row.FindControl("lblMaterialQty");

        DataTable dt = (DataTable)Session["dt"];
        
        if (dt.Rows.Count == 0)
        {
                
        }
        else
        {
          //here i do wrong     
           for (int i = 0; i < dt.Rows.Count; i++)
           {
               lblMaterial.Text = dt.Rows[i][1].ToString();
               lblMaterialValue.Text = dt.Rows[i][0].ToString();
               lblMaterialQty.Text = dt.Rows[i][2].ToString();
           }        }
    }
}


In my DataTable I have a 4 Rows butin my Grid it's Display Last Row only how to display all the rows can anyone help me ...


Thanks in Advance....
Posted

C#
Because u r using loop that is why every time your labels value r replaced by next one so at the last of loop , it contain only values of last row 
for (int i = 0; i < dt.Rows.Count; i++)
          {
              lblMaterial.Text = dt.Rows[i][1].ToString();
              lblMaterialValue.Text = dt.Rows[i][0].ToString();
              lblMaterialQty.Text = dt.Rows[i][2].ToString();
          }


tell what do u want ?
 
Share this answer
 
Hi pranathis012,
i think you are doing wrong. On RowdataBound event of gridview, You can only modify or change the already bind data on run time. i.e. if you want to do anything with rows of gridview on conditional basis like row formatting or any-thing.. so you can do. You should bind data table to Gridview using property

<pre lang="c#">
GridViewObject.DataSource=datatableObject;

and after binding you can change or modify with data.


Regards Rahul
 
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