TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Akhter HUssain
720
1.3k
102.3k
Row delete from database or Viewstate/Datatable
Jun 4 2020 1:59 PM
i have gridview,i want condition to delete row from gridview,
<asp:GridView ID=
"GridView1"
runat=
"server"
AllowSorting=
"True"
HorizontalAlign=
"Center"
AutoGenerateColumns=
"false"
CssClass=
"mygrdContent"
HeaderStyle-BackColor=
"#66ccff"
HeaderStyle-CssClass=
"header"
HeaderStyle-ForeColor=
"White"
OnRowDataBound=
"GridView1_RowDataBound"
OnRowDeleting=
"GridView1_RowDeleting"
PagerStyle-CssClass=
"pager"
RowStyle-CssClass=
"rows"
Width=
"452px"
ShowFooter=
"True"
>
<Columns>
<asp:TemplateField HeaderText=
"ID"
>
<ItemTemplate>
<asp:Label ID=
"D_CID"
runat=
"server"
Text=
'<%#Bind("D_CID")%>'
></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=
"Container"
>
<ItemTemplate>
<asp:Label ID=
"Contnam"
runat=
"server"
Text=
'<%#Bind("Contnam")%>'
></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=
"QTY"
>
<ItemTemplate>
<asp:Label ID=
"QTY"
runat=
"server"
Text=
'<%#Bind("QTY")%>'
></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID=
"lblTotal1"
runat=
"server"
></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
</ItemTemplate>
<EditItemTemplate>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType=
"Button"
ShowDeleteButton=
"True"
/>
</Columns>
<HeaderStyle BackColor=
"#66CCFF"
ForeColor=
"White"
/>
<SelectedRowStyle BackColor=
"Yellow"
/>
</asp:GridView>
if i have data in gridview in datatable/Viewstate then it delete from viewstate if first column id does not exit ,
if i have ID in first column ,which i retrieved from database then it deleted from database below i tried but it is not working
protected
void
GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int
id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
int
index = Convert.ToInt32(e.RowIndex);
DataTable dt = (DataTable)ViewState[
"dt"
];
// Delete from ViewState.
dt.Rows[index].Delete();
ViewState[
"dt"
] = dt;
string query =
"delete from tbl_Import_Detail WHERE D_CID = @Id"
;
using (SqlCommand cmd =
new
SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue(
"@Id"
, id);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
BindGridView();
Reply
Answers (
5
)
How can I insert data into a table using c#?
How to create multiple excel file with multiple sheet ?