selvi subramanian

selvi subramanian

  • NA
  • 799
  • 572.6k

delete the row in gridview using button click event

Jul 26 2014 6:07 AM
my source is
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
onrowdatabound="GridView1_RowDataBound"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text="<%#bind('id') %>"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text="<%#bind('Name') %>"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text="<%#bind('Status') %>"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField Text="Select" Visible="false"/>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Clear"
Width="168px" />
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
my code is
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowIndex == GridView1.SelectedIndex)
{
row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
DataTable dt = (DataTable)ViewState["DataTable"];
dt.Rows.Remove(row);
ViewState["DataTable"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
row.ToolTip = string.Empty;
}}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
my error is
Server Error in '/bramandam site' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1502: The best overloaded method match for 'System.Data.DataRowCollection.Remove(System.Data.DataRow)' has some invalid arguments
Source Error:
Line 78: row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
Line 79: DataTable dt = (DataTable)ViewState["DataTable"];
Line 80: dt.Rows.Remove(row);
Line 81: ViewState["DataTable"] = dt;
Line 82: GridView1.DataSource = dt;
Source File: d:\bramandam site\grd.aspx.cs Line: 80
Show Detailed Compiler Output:
Show Complete Compilation Source:
Version Information: Microsoft .NET Framework Version:2.0.50727.4984; ASP.NET Version:2.0.50727.4971

Answers (2)