Live Chat Icon For mobile
Live Chat Icon

How to delete a record using DataGrid

Platform: ASP.NET| Category: DataGrid

<asp:datagrid id='DataGrid1' runat='server' DataKeyField='Regionid' OnDeleteCommand='DataGrid1_Delete'
	 OnEditCommand='DataGrid1_Edit' OnCancelCommand='DataGrid1_Cancel'>
<Columns>
	<asp:ButtonColumn Text='Delete' CommandName='Delete' />
</Columns>
</asp:datagrid>
<asp:Label id='lblError' style='Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 160px' runat='server'
	Visible='False' ForeColor='Red'></asp:Label>

VB.NET


Dim mycn As SqlConnection
Dim myda As SqlDataAdapter
Dim ds As DataSet
Dim strConn, strSQL As String

Private Sub Page_Load(sender As Object, e As System.EventArgs)
	strConn = 'server=localhost;uid=sa;database=northwind;pwd=;'
		If Not Page.IsPostBack Then
			BindGrid()
		End If
End Sub ’Page_Load

Sub BindGrid()
	mycn = New SqlConnection(strConn)
	strSQL = 'Select * from Region'
	myda = New SqlDataAdapter(strSQL, mycn)
	ds = New DataSet()
	myda.Fill(ds, 'Table')
	DataGrid1.DataSource = ds
	DataGrid1.DataBind()
End Sub ’BindGrid

Public Sub DataGrid1_Cancel(sender As [Object], e As DataGridCommandEventArgs)
	DataGrid1.EditItemIndex = - 1
	BindGrid()
End Sub ’DataGrid1_Cancel

Public Sub DataGrid1_Edit(sender As [Object], e As DataGridCommandEventArgs)
	DataGrid1.EditItemIndex = CInt(e.Item.ItemIndex)
	BindGrid()
End Sub ’DataGrid1_Edit

Public Sub DataGrid1_Delete(sender As [Object], e As DataGridCommandEventArgs)
	Dim orderid As Integer = CInt(DataGrid1.DataKeys(CInt(e.Item.ItemIndex)))
	Dim deleteCmd As [String] = 'DELETE from Region where Regionid = @Regionid  '
	Dim cn As New SqlConnection(strConn)
	Dim myCommand As New SqlCommand(deleteCmd, cn)
	myCommand.Parameters.Add(New SqlParameter('@Regionid', SqlDbType.Int))
	myCommand.Parameters('@Regionid').Value = DataGrid1.DataKeys(CInt(e.Item.ItemIndex))
	myCommand.Connection.Open()
	Try
		myCommand.ExecuteNonQuery()
	Catch
		lblError.Text  = 'ERROR: Could not delete record'
	End Try
	myCommand.Connection.Close()
	BindGrid()
End Sub ’DataGrid1_Delete

C#


SqlConnection mycn;
SqlDataAdapter myda;
DataSet ds;
string strConn,strSQL;

private void Page_Load(object sender, System.EventArgs e)
{
	strConn ='server=localhost;uid=sa;database=northwind;pwd=;';
	if (!Page.IsPostBack )
	{
		BindGrid();
	}
}

void BindGrid()
{
	mycn = new SqlConnection(strConn);
	strSQL = 'Select * from Region'  ;
	myda = new SqlDataAdapter (strSQL, mycn);
	ds= new DataSet ();
	myda.Fill (ds,'Table');
	DataGrid1.DataSource =ds;
	DataGrid1.DataBind (); 
}

public void DataGrid1_Cancel(Object sender, DataGridCommandEventArgs e)
{
	DataGrid1.EditItemIndex = -1;
	BindGrid();
}

public void DataGrid1_Edit(Object sender, DataGridCommandEventArgs e)
{
	DataGrid1.EditItemIndex = (int)e.Item.ItemIndex;			 
	BindGrid(); 
}

public void DataGrid1_Delete(Object sender, DataGridCommandEventArgs e)
{
	int orderid=(int) DataGrid1.DataKeys[(int)e.Item.ItemIndex];;
	String deleteCmd = 'DELETE from Region where Regionid = @Regionid  ';
	SqlConnection cn = new SqlConnection (strConn);
	SqlCommand myCommand = new SqlCommand(deleteCmd, cn);
	myCommand.Parameters.Add(new SqlParameter('@Regionid', SqlDbType.Int ));
	myCommand.Parameters['@Regionid'].Value = DataGrid1.DataKeys[(int)e.Item.ItemIndex];
 	myCommand.Connection.Open();
	try
	{
		myCommand.ExecuteNonQuery();
	}
	catch (SqlException)
	{
		lblError.Text  = 'ERROR: Could not delete record';
	}
	myCommand.Connection.Close();
	BindGrid();
}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.