Inserting, Deleting and Updating in DataTable is very important in the real senario, with out interation to database every time.
So first i am creating a datatable in the pageload and this datatable is binded to gridview.
Before that declare static datatable globally as shown below
static DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dt = new DataTable();
dt.Columns.Add("S.NO");
dt.Columns.Add("Name");
dt.Columns.Add("Sal");
for (int i = 0; i < 10; i++)
{
dr = dt.NewRow();
dr["S.No"] = (1 + i);
dr["Name"] = "Sample " + (i+1);
dr["Sal"] = "27000";
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
The GUI of the web page looks as shown below.
ADD
As shown in the above figure, There are 3 buttons for ADD, Update and delete.
Now in the ADD button click event write the folowing code, before that declare DataRow dr1 as globally.
dr1 = dt.NewRow();
dr1["S.NO"] = TextBox1.Text;
dr1["Name"] = TextBox2.Text;
dr1["Sal"] = TextBox3.Text;
dt.Rows.Add(dr1);
GridView1.DataSource = dt;
GridView1.DataBind();
UPDATE
Now in the update button click event write the following code to update Datatable, before that declare globally a variable as DataEows[] rows;
rows = dt.Select();
for (int i = 0; i < rows.Length; i++)
{
if (i+1 == int.Parse(TextBox4.Text))
{
rows[i]["Name"] = TextBox5.Text;
rows[i]["Sal"] = TextBox6.Text;
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
Delete
Now in the Final button i.e., Delete button event write the following code to delete
int val = (int.Parse(TextBox7.Text) - 1);
dt.Rows.RemoveAt(val);
GridView1.DataSource = dt;
GridView1.DataBind();
And finally you can see all the 3 attempts as shown in the below figure
DownLoad Source Code from Here
So first i am creating a datatable in the pageload and this datatable is binded to gridview.
Before that declare static datatable globally as shown below
static DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dt = new DataTable();
dt.Columns.Add("S.NO");
dt.Columns.Add("Name");
dt.Columns.Add("Sal");
for (int i = 0; i < 10; i++)
{
dr = dt.NewRow();
dr["S.No"] = (1 + i);
dr["Name"] = "Sample " + (i+1);
dr["Sal"] = "27000";
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
The GUI of the web page looks as shown below.
ADD
As shown in the above figure, There are 3 buttons for ADD, Update and delete.
Now in the ADD button click event write the folowing code, before that declare DataRow dr1 as globally.
dr1 = dt.NewRow();
dr1["S.NO"] = TextBox1.Text;
dr1["Name"] = TextBox2.Text;
dr1["Sal"] = TextBox3.Text;
dt.Rows.Add(dr1);
GridView1.DataSource = dt;
GridView1.DataBind();
UPDATE
Now in the update button click event write the following code to update Datatable, before that declare globally a variable as DataEows[] rows;
rows = dt.Select();
for (int i = 0; i < rows.Length; i++)
{
if (i+1 == int.Parse(TextBox4.Text))
{
rows[i]["Name"] = TextBox5.Text;
rows[i]["Sal"] = TextBox6.Text;
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
Delete
Now in the Final button i.e., Delete button event write the following code to delete
int val = (int.Parse(TextBox7.Text) - 1);
dt.Rows.RemoveAt(val);
GridView1.DataSource = dt;
GridView1.DataBind();
And finally you can see all the 3 attempts as shown in the below figure
DownLoad Source Code from Here
its very nice to understand
ReplyDeleteThank You Biyyapu Suneel
ReplyDelete