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 isServer Error in '/bramandam site' Application.Compilation ErrorDescription: 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 argumentsSource 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: 80Show Detailed Compiler Output:Show Complete Compilation Source:Version Information: Microsoft .NET Framework Version:2.0.50727.4984; ASP.NET Version:2.0.50727.4971 |

selvi subramanianPosted Jul 27, 2014, 3:21 AM
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if(GridView1.SelectedIndex > 0)
{
if (row.RowIndex == GridView1.SelectedIndex)
{
DataTable DataTable1 = new DataTable();
string query = "select Id, Name, Status FROM aten";
string constr = ConfigurationManager.AppSettings["s"].ToString();
SqlDataAdapter da = new SqlDataAdapter(query, constr);
da.Fill(DataTable1);
HiddenField hdnID = row.FindControl("HiddenField1") as HiddenField;
Label Id = row.FindControl("Label4") as Label;
DataRow[] drArr = DataTable1.Select("Id=" + hdnID);
if (drArr.Length > 0)
{
DataTable1.Rows.Remove(drArr[0]);
ViewState["DataTable"] = DataTable1;
}
}
}
}
}
my error is
Server Error in '/bramandam site' Application.
Cannot find column [System.Web.UI.WebControls.HiddenField].
Description: An unhandled exception occurred during the execution of the current web request.
Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.EvaluateException: Cannot find column [System.Web.UI.WebControls.HiddenField].
Source Error: Line 88: Label Id = row.FindControl("Label4") as Label;
Line 89: Line 90: DataRow[] drArr = DataTable1.Select("Id=" + hdnID);
Line 91:
Line 92: if (drArr.Length > 0)
Source File: d:\bramandam site\grd.aspx.cs Line: 90
Stack Trace: [EvaluateException: Cannot find column [System.Web.UI.WebControls.HiddenField].]
System.Data.NameNode.Bind(DataTable table, List`1 list) +1187869
System.Data.BinaryNode.Bind(DataTable table, List`1 list) +42
System.Data.DataExpression.Bind(DataTable table) +59
System.Data.DataExpression..ctor(DataTable table, String expression, Type type) +4822791
System.Data.Select..ctor(DataTable table, String filterExpression, String sort, DataViewRowState recordStates) +89 System.Data.DataTable.Select(String filterExpression) +61 grd.Button1_Click(Object sender, EventArgs e) in d:\bramandam site\grd.aspx.cs:90
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
Version Information: Microsoft .NET Framework Version:2.0.50727.4984; ASP.NET Version:2.0.50727.4971
Pradeep ShetPosted Jul 26, 2014, 9:30 AM
This will definitely give you compilation error because you re trying to remove GridViewRow object from DataTable. DataTable consists of DataRow and not GridViewRow.
foreach(GridViewRow row in GridView1.Rows)
{
if(row.RowIndex == GridView1.SelectedIndex)
{
//Store some unique id in the row while binding
HiddenField hdnID = row.FindControl("hdid") as HiddenField;
DataRow[] drArr = DataTable1.Select("Id=" + hdnID );
if(drArr.Length > 0)
{
DataTable1.Rows.Remove(drArr[0]);
ViewState["DataTable"] = dt;}
}
}
Hope tihs solves your problem, if so plz mark it answered..