Free Training


C Language  |  CSS  |  MainFrame  |  VBScript  |  PHP  |  XML  |  C++ Tutorials  |  Ajax  |  JavaScript  |  CSS3  |  UML  |  jQuery  |  Microsoft AJAX

ASP.NET Tutorials

 
Home Tutorials ASP.NET
 

ASP.NET Using the DataList and Repeater, Datagrid Controls

 

Page 4 of 2


Okay now we come to a slightly difficult step. We will carefully look at the Update method and see how it works.


private void Update_DataGrid(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
System.Web.UI.WebControls.TextBox cName = new System.Web.UI.WebControls.TextBox();
cName = (System.Web.UI.WebControls.TextBox) e.Item.Cells[1].Controls[0];
SqlCommand myCommand = new SqlCommand("SP_UpdatePerson",myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Add(new SqlParameter("@PersonName",SqlDbType.NVarChar,50));
myCommand.Parameters["@PersonName"].Value = cName.Text;
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
myDataGrid.EditItemIndex = -1;
BindData();
}


Lets now dig into this method and see whats going on.



  • The name of the method as you can see is Update_DataGrid, this event is fired when you click the update link button which appears after clicking the edit button.
  • We declare a variable of TextBox type and call it cName. The reason of declaring a TextBox is that the value that we want is inside the TextBox which is inside the DataGrid control.
  • Later we made the SqlCommand object which takes stored procedure "SP_UpdatePerson", which will be discussed afterwords.
  • After marking the command object with the stored procedure we passed the parameter which is PersonName.
  • Finally we execute the Query and set the editItemIndex property of the DataGrid '-1' which will bring the datagrid back to its original form i.e without any textboxes.
  • Don't forget to bind the datagrid.

Update Stored Procedure


CREATE PROCEDURE SP_UpdatePerson
@PersonName nvarchar(50)
AS
UPDATE tblPerson SET PersonName = @PersonName WHERE PersonName = @PersonName;


CREATE PROCEDURE SP_UpdatePerson
@PersonName nvarchar(50)
AS
UPDATE tblPerson SET PersonName = @PersonName WHERE PersonName = @PersonName;

Selecting Item from the Datagrid:


Another cool feature of the Datagrid control is that you can select any row from the datagrid and it will be displayed as the highligted row in the grid.


The highlight row event is called SelectedIndexChanged event. The event is called when the select column is clicked. The select column can be added to the datagrid using the property builder, just like we added "edit/cancel/update" link buttons.


// This event is fired when the Select is clicked
private void Select_DataGrid(object sender, System.EventArgs e)
{
// prints the value of the first cell in the DataGrid
Label2.Text += myDataGrid.SelectedItem.Cells[0].Text;
}



This method is pretty simple. When the datagrid select link button is pressed. We retrieve the item from the datagrid which is residing on the same row on which the link button is pressed. As we can see above in the code that we are retrieving the value from the first column of the datagrid.


I hope you all liked the article happy programming !



Download Sample Code



This is the Header




This is the footer





Read Next: Managing Data with ADO.NET DataSets and C#



 

 

Comments


hjeyel said:

  ::) ::) :) that helped
August 8, 2006, 1:23 am

jayakrishna08 said:

  what is the code to apply paging to GridView in c# .net 2005.I tried with GridView1.AllowPaging=true, but o use
March 14, 2007, 1:08 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Weekly Offers

Sponsored Links