如何在DataTable中的gridview中的标签中添加文本

时间:2012-09-28 11:06:33

标签: c# asp.net gridview label

我想从DataTable绑定网格视图

    var dt = new DataTable();
    dt.Columns.Add("nrtest");
    dt.Columns.Add("asd");
    dt.Columns.Add("dsa");
    dt.Columns.Add("qwe");
    dt.Columns.Add("ewq");

    dt.Rows.Add("test1","","","","");
    dt.Rows.Add("test2","","","","");
    dt.Rows.Add("test3","","","","");


    mygrid.DataSource = dt;
    mygrid.DataBind();

网格中的第一列包含标签

                                                                                                                                                  

我想将第一列从datatable添加到gridview的第一列

我这样想:

 foreach(GridViewRow gdRow in mygrid.Rows)
            {
               if (gdRow.RowType == DataControlRowType.DataRow)
                     {
             var label1 = (Label) gdRow.Cells[1].FindControl("myLabel");
                         text1.Text = text1;

             var label2 = (Label) gdRow.Cells[1].FindControl("myLabel");
                         text2.Text = text2;

             var label3 = (Label) gdRow.Cells[1].FindControl("myLabel");
                         text3.Text = text3;
           }
      }
  but it doesn't work, at the end all the labels contain text3

我的网格视图:

 <asp:GridView ID="mygrid" runat="server" AutoGenerateColumns="False" 
              Visible="True" width="600px" onrowcreated="GdOrarRowCreated">

          <Columns>
                      <asp:TemplateField >
                          <HeaderTemplate>
                   <asp:Label ID="text" runat="server" Text="text" ></asp:Label>
                          </HeaderTemplate>
                          <ItemTemplate>
                              <asp:Label runat="server" ID="myLabel" />
                          </ItemTemplate>
                      </asp:TemplateField>

  <asp:BoundField DataField="asd" Visible="True"  HeaderText="col2"/>
  <asp:BoundField DataField="dsa" Visible="True"  HeaderText="col3"/>
  <asp:BoundField DataField="qwe" Visible="True"  HeaderText="col4"/>
  <asp:BoundField DataField="ewq" Visible="True"  HeaderText="col5"/>

                       </Columns>
                    </asp:GridView>

2 个答案:

答案 0 :(得分:2)

您要为所有3个标签指定相同的ID。给他们一个唯一的ID,然后尝试。因为它指定了最后一个值

您的代码还存在其他一些问题,您可以尝试类似

的内容
    var dt = new DataTable();
    dt.Columns.Add("nrtest");
    dt.Columns.Add("asd");
    dt.Columns.Add("dsa");
    dt.Columns.Add("qwe");
    dt.Columns.Add("ewq");

    dt.Rows.Add("test1", "", "", "", "");
    dt.Rows.Add("test2", "", "", "", "");
    dt.Rows.Add("test3", "", "", "", "");


    mygrid.DataSource = dt;
    mygrid.DataBind();
什么做什么然后绑定网格你可以使用for循环这将允许你更容易获得行索引,你可以绑定rowindex然后:

    var rows =  mygrid.Rows;
    for(int i=0,j=rows.Count; i<j;i++)
    {
        var row = rows[i];
        if (row.RowType == DataControlRowType.DataRow)
        {
            var label1 = (Label)row.Cells[1].FindControl("myLabel");
            //use rowindex i and colindex "nrtest" to get data from dt
            label1.Text = dt.Rows[i]["nrtest"].ToString();
        }
    }

网格代码将保持与问题相同

<asp:GridView ID="mygrid" runat="server" AutoGenerateColumns="False" Visible="True"
    Width="600px">
    <Columns>
        <asp:TemplateField>
            <HeaderTemplate>
                <asp:Label ID="text" runat="server" Text="text"></asp:Label>
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label runat="server" ID="myLabel" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="asd" Visible="True" HeaderText="col2" />
        <asp:BoundField DataField="dsa" Visible="True" HeaderText="col3" />
        <asp:BoundField DataField="qwe" Visible="True" HeaderText="col4" />
        <asp:BoundField DataField="ewq" Visible="True" HeaderText="col5" />
    </Columns>
</asp:GridView>

答案 1 :(得分:0)

您要为所有3个标签分配相同的ID。 给他们一个唯一的ID,然后尝试。