# Basic GridView backed with a DataTable

I need to add a list of names and (optional) links to my ASP.net page.

I learnt (hopefully) to use a GridView to do this, mostly working from the example given by this blog entry: adding-dynamic-rows-in-gridview.

I've added a GridView to my frontend:

<asp:GridView
AutoGenerateColumns="false" ShowFooter="true">
<Columns>
<ItemTemplate>
<asp:TextBox ID="Name" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<ItemTemplate>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button
CausesValidation="false" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


With the code behind:

private void setupInitialNameLinks()
{
/* store DataTable in the ViewState for preservation across PostBack's */
ViewState["CurrentTable"] = dt;
/* bind the displayed DataGrid to our DataTable */
/* set current TextBox values to the values in the DataTable
* surely the DataBind should do this for us?!? */
setTextBoxValuesToDataTableValues(dt);
}

{
DataTable dt = new DataTable();

{
foreach (var auth in existing)
{
DataRow r = dt.NewRow();
r["Name"] = auth.name;
}
}
else
{
}
return dt;
}

{
DataRow emptyRow = dt.NewRow();
emptyRow["Name"] = String.Empty;
return emptyRow;
}

{
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable) ViewState["CurrentTable"];

/* update our DataTable to the POST'ed data */
int numPostedRows = dt.Rows.Count - 1;
for (int i = 0; i < numPostedRows; i++)
{
TextBox tbName =
string name = tbName.Text; // get POST'ed value
dt.Rows[i]["Name"] = name; // set DataTable val to the POST'ed value
}

/* store our DataTable in the ViewState for preservation across PostBack */
ViewState["CurrentTable"] = dt;

/* bind the displayed DataGrid to our DataTable */

/* set current TextBox values to the values in the DataTable
* needs to happen after the DataBind()
* surely the DataBind should do this for us?!? */
setTextBoxValuesToDataTableValues(dt);

}
else
{
throw new Exception("CurrentTable is null!");
}
}

private void setTextBoxValuesToDataTableValues(DataTable dt)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
tbName.Text = dt.Rows[i]["Name"].ToString();
}
}


Does any of this code look troublesome, prone to error, umnaintanable, not the right way of doing it, etc?

One big bit of confusion I have (even though the code works) — why do I need to store the DataTable in the ViewState manually and set my TextBox displayed values manually (in setTextBoxValuesToDataTableValues)? Surely the GridView state should be maintained across the POST without me needing to worry about the ViewState and the DataBind should set the TextBox values automatically?

• The KetticGridView is capable of populating gridview with data using DataTable to a single data table or dataset, as well as to multiple data tables through hierarchical data binding.
– user36445
Feb 8, 2014 at 9:38

Regarding having to get/set the value of the textboxes, you should get to know the Bind expression. For instance, you could have:
<asp:TextBox ID="Name" runat="server" Text='<%# Bind("Name") %>' />