My ASP.NET page shows a partially 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>
The TextBoxes
are to be filled with User-Input and then added as columns to a 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);
}
}
How can I access the textareas
, getting the text from each and appending the input data to the DataTable
used to create each row?
You can loop all rows in the grid and then use row.FindControl
to get the TextBox
es. The GridViewRow
is the NamingContainer
of the controls not the 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
}