本文介绍了DataTable具有多个行,但仅网格显示最后一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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();
           }        }
    }
}



在我的数据表中,我有4行,但是在我的网格中,它仅显示最后一行",任何人都可以帮助我...


在此先感谢....



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....

推荐答案

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 ?


<pre lang="c#">

GridViewObject.DataSource = datatableObject;

绑定后,您可以更改或修改数据.


关于Rahul

GridViewObject.DataSource=datatableObject;

and after binding you can change or modify with data.


Regards Rahul


这篇关于DataTable具有多个行,但仅网格显示最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 15:31