从gridview中的textarea获取数据作为数据表的列附加

时间:2014-09-23 13:50:33

标签: c# asp.net gridview datagridviewtextboxcell

我的ASP.NET页面显示部分databound gridview:

<asp:GridView ID="myGridView" runat="server" AutoGenerateColumns="False">
  <Columns>
    <asp:BoundField DataField="name" HeaderText="Name" />
    <asp:TemplateField HeaderText="Datum" ItemStyle-CssClass="tdRight">
      <ItemTemplate>
        <asp:TextBox ID="datum" runat="server" Text=""></asp:TextBox>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Uhrzeit" ItemStyle-CssClass="tdRight">
      <ItemTemplate>
        <asp:TextBox ID="uhrzeit" runat="server" Text=""></asp:TextBox>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Ort" ItemStyle-CssClass="tdRight">
      <ItemTemplate>
        <asp:TextBox ID="ort" runat="server" Text=""></asp:TextBox>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Hausnummer" ItemStyle-CssClass="tdRight">
      <ItemTemplate>
        <asp:TextBox ID="hausnummer" runat="server" Text=""></asp:TextBox>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Gebäude" ItemStyle-CssClass="tdRight">
      <ItemTemplate>
        <asp:TextBox ID="gebaeude" runat="server" Text=""></asp:TextBox>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Raumnummer" ItemStyle-CssClass="tdRight">
      <ItemTemplate>
        <asp:TextBox ID="raumnummer" runat="server" Text=""></asp:TextBox>
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>

TextBoxes将填充用户输入,然后作为列添加到DataTable

DataTable inputData = new DataTable();
inputData.Columns.Add(new DataColumn("datum", typeof(String)));
inputData.Columns.Add(new DataColumn("uhrzeit", typeof(String)));
inputData.Columns.Add(new DataColumn("ort", typeof(String)));
inputData.Columns.Add(new DataColumn("hausnummer", typeof(String)));
inputData.Columns.Add(new DataColumn("gebaeude", typeof(String)));
inputData.Columns.Add(new DataColumn("raumnummer", typeof(String)));
foreach(DataRow row in myGridView.Rows) {
  foreach(DataColumn col in row.ItemArray) {
    DataRow dr = inputData.NewRow();
    dr["datum"] = ((TextBox) myGridView.FindControl("datum")).Text;
    dr["uhrzeit"] = ((TextBox) myGridView.FindControl("uhrzeit")).Text;
    dr["ort"] = ((TextBox) myGridView.FindControl("ort")).Text;
    dr["hausnummer"] = ((TextBox) myGridView.FindControl("hausnummer")).Text;
    dr["gebaeude"] = ((TextBox) myGridView.FindControl("gebaeude")).Text;
    dr["raumnummer"] = ((TextBox) myGridView.FindControl("raumnummer")).Text;
    inputData.Rows.Add(dr);
    printRow(dr);
  }
}

如何访问textareas,从每个文本中获取文本并将输入数据附加到用于创建每一行的DataTable

1 个答案:

答案 0 :(得分:2)

您可以循环网格中的所有行,然后使用row.FindControl获取TextBox es。 GridViewRow是控件的NamingContainer而不是GridView

foreach (GridViewRow row in myGridView.Rows)
{
    DataRow newRow = inputData.Rows.Add();
    newRow.SetField("datum", ((TextBox)row.FindControl("datum")).Text);
    // and so on...
    // note that you don't need to add the row since it's already added
}