470,849 Members | 1,175 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

Dynamic Creation of GridView using DataTable

I m using this code in C#.net, for dynamic creation of GridView without using DB.

<CODE>


protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Session["myDatatable"] = CreateDataTable();

GridView1.DataSource = ((DataTable)Session["myDatatable"]).DefaultView;
GridView1.DataBind();
}
}


private void myDataGrid_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
}
private DataTable CreateDataTable()
{
DataTable myDataTable = new DataTable();

DataColumn myDataColumn;

myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "User Name";
myDataTable.Columns.Add(myDataColumn);

myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "First Name";
myDataTable.Columns.Add(myDataColumn);

return myDataTable;

}
private void AddDataToTable(string username, string firstname, string lastname, DataTable myTable)
{
DataRow row;

row = myTable.NewRow();

row["User Name"] = username;
row["First Name"] = firstname;

myTable.Rows.Add(row);
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text.Trim() == "")
{
Label1.Text = "You must fill a username.";
return;
}
else
{
AddDataToTable(TextBox1.Text.Trim(), TextBox2.Text.Trim(),Label2.Text, (DataTable)Session["myDatatable"]);

GridView1.DataSource = ((DataTable)Session["myDatatable"]).DefaultView;
GridView1.DataBind();

TextBox2.Text = "";
TextBox1.Text = "";
Label1.Text = "";

}
}


</CODE>

Can any one tell how to delete data in gridview,.....

Thanks in Advance
With Regards
Tina
Apr 2 '08 #1
0 1528

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Richard Carpenter | last post: by
4 posts views Thread by Ken Wigle | last post: by
4 posts views Thread by Tomasz Jastrzebski | last post: by
3 posts views Thread by Lloyd Sheen | last post: by
5 posts views Thread by =?Utf-8?B?QWRhciBXZXNsZXk=?= | last post: by
reply views Thread by Craig Buchanan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.