Hai
i doing project in c#2008 web application, i having gridview colums contain Groupname,First name,Lastname....like that.
i want to delete perticular row in grid view. please give me only c# coding to delete row,don't want asp coding..pls urgent...
i need only c# coding please......
Loading
Abhimanyu K VatsaPosted May 6, 2011, 4:21 AM
<%@ Page Language="VB" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>title>
head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView
ID="GridView1"
runat="server"
AutoGenerateColumns="False"
DataSourceID="SqlDataSource1"
EmptyDataText="There are no data records to display."
Width="788px"
DataKeyNames="ID">
<Columns>
<asp:BoundField
DataField="ID"
HeaderText="ID"
SortExpression="ID" />
<asp:BoundField
DataField="NAME"
HeaderText="NAME"
SortExpression="NAME" />
<asp:BoundField
DataField="ADDRESS"
HeaderText="ADDRESS"
SortExpression="ADDRESS" />
<asp:BoundField
DataField="MOBILE"
HeaderText="MOBILE"
SortExpression="MOBILE" />
<asp:CommandField
ButtonType="Image"
ShowEditButton="true"
EditText="Edit Movie"
EditImageUrl="~/images/edit.GIF"
UpdateText="Update Movie"
updateimageurl="~/images/update.GIF"
ShowCancelButton="true"
CancelText="Cancel Edit"
cancelimageurl="~/images/cancel.GIF"
ShowDeleteButton="true"
DeleteText="Delete Movie"
Deleteimageurl="~/images/delete.GIF"/>
Columns>
asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>"
DeleteCommand="DELETE FROM MYTB WHERE ID=@ID"
ProviderName="<%$ ConnectionStrings:DatabaseConnectionString1.ProviderName %>"
SelectCommand="SELECT [ID], [NAME], [ADDRESS], [MOBILE] FROM [MYTB]"
UpdateCommand="UPDATE MYTB SET ID =@ID, NAME =@NAME, ADDRESS =@ADDRESS, MOBILE =@MOBILE WHERE ID=@ID">
asp:SqlDataSource>
div>
form>
body>
html>
For more Read this
http://www.vbdotnetheaven.com/UploadFile/abhikumarvatsa/2249/
Ankit NandekarPosted May 6, 2011, 2:39 AM
1. on Gridview do AutoGenerateColumns="False".
2.use TemplateField and itemtemplate to show ur data .
3.do
BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px"
CellPadding="4">
Here UserId ,UserName is table field name if u dont want to show Userid then do it as visible false or if u want then use one more templete field to show id which is unqiue which will help us to delete perticular record.
4. Now genrate gridview's onrowdeleting event by going its property click on events u can see onrowdeleting events is ther double click on it.
5.write following code on it
protected void gvDeleteUsers_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Label lblUserId = (Label)gvDeleteUsers.Rows[e.RowIndex].Cells[0].FindControl("lblUserId");
SqlConnection con = new SqlConnection("Data Source=C3;Initial Catalog=PhotoGallary;Persist Security Info=True;User ID=sa;Password=p@ssw0rd");
SqlCommand cmd = new SqlCommand("DELETE FROM Registration_Table WHERE UserId=" + Convert.ToInt32(lblUserId.Text),con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
ShowData();
}
public void ShowData()
{
write code to bind database with gridview.........
}
Note: Add Namespace using System.Data.SqlClient
Dinesh BeniwalPosted May 6, 2011, 2:22 AM