Saturday, 29 August 2009

Playing With GridView (Part 2)

Hello Friends.......

In the first part, we learned how to bind data to Gridview and how to select,update
and sort records.In this final part,we will learn the last two very important operations on Gridview that is : adding new records and deleting existing records from the GridView.

First of all to add new records in to our grid, we need to create textboxes it our grid where we can insert new record.For that perpose,we create footer template with textbox after the edit tepmlate of each column in the following manner :


< FooterTemplate>
< asp:TextBox id="footertxtid" runat="server">
< /FooterTemplate>



...........And at the end we create a template field for 'save' and 'cancel' commands in the following manner :

< showheader="False">
< ItemTemplate>
< /ItemTemplate>
< FooterTemplate>
< asp:LinkButton id="footerlnksave" runat="server" text="Save" commandname="Save">
< /asp:LinkButton>
< asp:LinkButton id="lnkCancel" runat="server" text="Cancel" commandname="Cancel">
< /asp:LinkButton>
< /FooterTemplate>
< /asp:TemplateField>


and we add a button for inserting the record at the bootom oof our gridview control.

...........After creating the templates, Our gridview will look like this,at run time :




..............Now we will do some coding on the click event of our add button :

protected void btnAdd_Click(object sender, EventArgs e)
{
GridView1.FooterRow.Visible = true;
TextBox name = (TextBox)GridView1.FooterRow.FindControl("footertxtid");
name.Focus();
}

............And Some coding on the RowCommand event of our gridview control as follows :

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Save"))
{
TextBox name = (TextBox)GridView1.FooterRow.FindControl("footertxtid");
TextBox desg = (TextBox)GridView1.FooterRow.FindControl("footertxtid1");
TextBox comp = (TextBox)GridView1.FooterRow.FindControl("footertxtid2");
TextBox exep = (TextBox)GridView1.FooterRow.FindControl("footertxtid3");
if (name.Text != "")
{
con.Open();
string str = "insert into Profile values ('" + name.Text + "','" + desg.Text + "','" + comp.Text + "','" + exep.Text + "')";
SqlCommand com = new SqlCommand(str, con);
com.ExecuteNonQuery();
con.Close();
BindGrid();
}
}
}

..............Now run the application and click on the add button.the footer template will be visible and you will find four new text boxes.Now insert some data into the text boxes and then click on the 'save' link button.................


...............Your new record has been inserted now.You can check it by changing page index :


..............Now we will add the 'Delete' operation to our gridview. just add the delete button to the gridview in the same manner as we added the edit button.And now add the following code to the Rowdeleting event of our grid :

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string NameOld = Convert.ToString(GridView1.DataKeys[e.RowIndex].Value); con.Open();
string str = "delete from Profile where Name = '" + NameOld + "' ";
SqlCommand com = new SqlCommand(str, con);
com.ExecuteNonQuery();
GridView1.EditIndex = -1;
con.Close();
BindGrid();
}

............Now run your application, select a frecord and click on the 'delete' link button,the selected record would get deleted.



..............So we have just created a fully functional GridView which supports adition,deletion,updation,cancel,sorting,paging, all the operation in a single control.
Now use some creativity along with some logic and make it look more attractive and more functional..........Our final grid looks like this :


So Best Of Luck And HAVE FUN.............................

Download Full Source Code [C#/VB.Net]


GridView
GridView.rar
Hosted by eSnips

No comments: