4
Answers

Add columns then bind rows in Grid view using Data table.

Anurag Singh

Anurag Singh

9y
431
1
Hi,
 
I want to add columns and then bind rows using Data table and show in Grid view  without using Database(DONE).
After that I want a VIEW/EDIT functionality means if I click any row then the data of row should be in their respective controls(textboxes)  and I can update them and again save.
 
For better understand, I am giving my .cs  and .aspx page code.
 
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
if (!this.IsPostBack)
{
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Name"), new DataColumn("Address") });
ViewState["Customers"] = dt;
this.BindGrid();
}
}
protected void BindGrid()
{
GridView1.DataSource = (DataTable)ViewState["Customers"];
GridView1.DataBind();
}
protected void btnAdd_Click(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["Customers"];
dt.Rows.Add(txtName.Text.Trim(), txtAddress.Text.Trim());
ViewState["Customers"] = dt;
this.BindGrid();
txtName.Text = string.Empty;
txtAddress.Text = string.Empty;
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
==================================================================
 
<form id="form1" runat="server">
<div>
<table>
<tr>
<td style="font-weight: bold">
<asp:Label ID="lblName" runat="server" Text="Name : "></asp:Label>
</td>
<td>
<asp:TextBox ID="txtName" runat="server" />
</td>
<td>
<asp:RequiredFieldValidator runat="server" ID="rfvName" ErrorMessage="Please Enter This Filed." ControlToValidate="txtName"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td style="font-weight: bold">
<asp:Label ID="lblAddress" runat="server" Text="Address : "></asp:Label>
</td>
<td>
<asp:TextBox ID="txtAddress" runat="server" />
</td>
<td>
<asp:RequiredFieldValidator runat="server" ID="rfvAddress" ErrorMessage="Please Enter This Filed." ControlToValidate="txtAddress"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td></td>
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
</td>
</tr>
</table>
<asp:GridView ID="GridView1" runat="server" CssClass="Grid" AutoGenerateColumns="false" OnRowEditing="GridView1_RowEditing" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="120" />
<asp:BoundField DataField="Address" HeaderText="Address" ItemStyle-Width="120" />
<asp:ButtonField Text="View" CommandName="View">
<ControlStyle Font-Bold="True" Font-Underline="true" ForeColor="Green" />
</asp:ButtonField>
</Columns>
</asp:GridView>
<br />
</div>
</form>
 
 
Regards:
Anurag Singh 
Answers (4)