简体   繁体   中英

DataTable cannot DataBind() to GridView (after sucessfully deleting a row from DataTable)

I am successfully able to delete the row from the DataTable from Modal Popup Extender button click event at ButtonOk_Click().

But showing error when DataBind() again to GridView at ButtonOk_Click()

I am receiving ERROR at below line in CustomersGridView_RowDataBound if (Convert.ToInt32(e.Row.Cells[10].Text) == 0)

May be because e.Row.Cells[10].Text is not getting value appropriately when data bound after deleting.

ASPX Page

 <asp:ScriptManagerID="ScriptManager1"runat="server"> </asp:ScriptManager> <asp:GridViewID="grdOrgListData"runat="server"AllowPaging="True"AutoGenerateColumns="False"onrowdatabound="CustomersGridView_RowDataBound" CssClass="tabletable-bordered"OnPageIndexChanging="grdOrgListData_PageIndexChanging"> <Columns> <asp:BoundFieldDataField="OrgID"HeaderText="ID"/> <asp:BoundFieldDataField="OrgName"HeaderText="Name"ReadOnly="True"/> <asp:BoundFieldDataField="OrgDescription"HeaderText="Description"/> <asp:BoundFieldDataField="OrgAddress"HeaderText="Address"/> <asp:BoundFieldDataField="OrgZipCode"HeaderText="ZipCode"/> <asp:BoundFieldDataField="OrgCountry"HeaderText="Country"ReadOnly="True"/> <asp:BoundFieldDataField="OrgEmail"HeaderText="Email"ReadOnly="True"/> <asp:BoundFieldDataField="OrgPhoneNo"HeaderText="PhoneNo"/> <asp:BoundFieldDataField="OrgWebsite"HeaderText="Website"ReadOnly="True"/> <asp:BoundFieldDataField="EmailID"HeaderText="OwnerEmail"/> <asp:BoundFieldDataField="ActiveStatus"HeaderText="ActiveStatus"/> <asp:TemplateFieldHeaderText="Actions"> <ItemTemplate> <asp:ImageButtonrunat="server"ID="imgBtnView"OnClick="imgBtnView_Click"/> <asp:ImageButtonrunat="server"ID="imgBtnEdit"OnClick="imgBtnEdit_Click"/> <asp:ImageButtonrunat="server"ID="imgBtnDelete"OnClick="imgBtnDelete_Click"/> <asp:ImageButtonrunat="server"ID="imgBtnActive"OnClick="imgBtnActive_Click"/> </ItemTemplate> </asp:TemplateField> </Columns> <editrowstyleBackColor="#999999"></editrowstyle> <footerstyleBackColor="#5D7B9D"Font-Bold="True"ForeColor="White"></footerstyle> <headerstyleBackColor="#5D7B9D"Font-Bold="True"ForeColor="White"></headerstyle> <rowstyleBackColor="#F7F6F3"ForeColor="#333333"></rowstyle> <selectedrowstyleBackColor="#E2DED6"Font-Bold="True"ForeColor="#333333"></selectedrowstyle> <sortedascendingcellstyleBackColor="#E9E7E2"></sortedascendingcellstyle> <pagersettingsmode="Numeric"position="Bottom"pagebuttoncount="10"/> <pagerstyleHorizontalAlign="right"CssClass="pagination-ys"/> </asp:GridView> <asp:ButtonID="btnShowPopupView"runat="server"style="display:none"/> <asp:ButtonID="btnShowPopupDelete"runat="server"style="display:none"/> <ajaxToolkit:ModalPopupExtenderID="ModalPopupExtender1"runat="server"TargetControlID="btnShowPopupView"Drag="true" PopupControlID="PNLView"OkControlID="ButtonClose"BackgroundCssClass="modalBackground"/> <ajaxToolkit:ModalPopupExtenderID="ModalPopupExtender2"runat="server"TargetControlID="btnShowPopupDelete"Drag="true" PopupControlID="PNLDelete"OkControlID="ButtonOk"CancelControlID="ButtonCancel"BackgroundCssClass="modalBackground"/> <asp:PanelID="PNLView"runat="server"Style="display:none;width:350px;background-color:White;border-width:2px;border-color:Black;border-style:solid;padding:20px;"> OrgID:<asp:LabelID="lblOrgID"runat="server"></asp:Label><br/> OrgName:<asp:LabelID="lblOrgName"runat="server"></asp:Label><br/> Desciption:<asp:LabelID="lblOrgDescription"runat="server"></asp:Label><br/> MailAddress:<asp:LabelID="lblOrgAddress"runat="server"></asp:Label><br/> ZipCode:<asp:LabelID="lblOrgZipCode"runat="server"></asp:Label><br/> Country:<asp:LabelID="lblOrgCountry"runat="server"></asp:Label><br/> Email:<asp:LabelID="lblOrgEmail"runat="server"></asp:Label><br/> PhoneNo:<asp:LabelID="lblOrgPhoneNo"runat="server"></asp:Label><br/> Website:<asp:LabelID="lblOrgWebsite"runat="server"></asp:Label><br/> OwnerEmail:<asp:LabelID="lblOwnerEmail"runat="server"></asp:Label><br/> ActiveStatus:<asp:LabelID="lblActiveStatus"runat="server"></asp:Label><br/> <br/> <br/> <divstyle="text-align:right;"> <asp:ButtonID="ButtonClose"runat="server"Text="Close"CssClass="btnbtn-smbtn-primarym-tn-xs"/> </div> </asp:Panel> <asp:PanelID="PNLDelete"runat="server"Style="display:none;width:300px;background-color:White;border-width:2px;border-color:Black;border-style:solid;padding:20px;"> OrgName:<asp:LabelID="lblOrgDelete"runat="server"></asp:Label> <br/> Doyouwanttodeletethisorganisation? <br/> <br/> <divstyle="text-align:right;"> <asp:ButtonID="ButtonOk"runat="server"Text="OK"CssClass="btnbtn-smbtn-primarym-tn-xs" OnClick="ButtonOk_Click"UseSubmitBehavior="false"/> <asp:ButtonID="ButtonCancel"runat="server"Text="Cancel"CssClass="btnbtn-smbtn-primarym-tn-xs"/> </div> </asp:Panel> 

===========================================

CS Page

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.Sql; using System.Data.SqlClient; using System.Windows.Forms; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Diagnostics; using System.Drawing; namespace PseudoExam.AdminPanel { public partial class Organisation : System.Web.UI.Page { protected void btnAdd_click(object sender, EventArgs e) { Server.Transfer("/AdminPanel/Organisation.Add.aspx"); } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) LoadGridData(); } private void LoadGridData() { string strOrgAdminIDs = Convert.ToString(Session["OrgAdminIDs"]); DataTable ds = DBOperations.GetOrgDataset(strOrgAdminIDs); ds.PrimaryKey = new DataColumn[] { ds.Columns["OrgID"] }; string strLabel = "Showing 0 to 0 of 0 records"; Session["OrgDataSet"] = ds; if (ds != null) { if (ds.Rows.Count != 0) { grdOrgListData.DataSource = ds; grdOrgListData.DataBind(); grdOrgListData.Columns[0].Visible = false; grdOrgListData.Columns[2].Visible = false; grdOrgListData.Columns[3].Visible = false; grdOrgListData.Columns[4].Visible = false; grdOrgListData.Columns[7].Visible = false; grdOrgListData.Columns[9].Visible = false; grdOrgListData.Columns[10].Visible = false; if (ds.Rows.Count > 0 && ds.Rows.Count > 9) strLabel = "Showing 1 to 10 of " + ds.Rows.Count + " records"; else if (ds.Rows.Count > 0 && ds.Rows.Count <= 9) strLabel = "Showing 1 to " + ds.Rows.Count + " of " + ds.Rows.Count + " records"; } else { grdOrgListData.DataSource = null; grdOrgListData.DataBind(); } } lblRecordStatus.Text = strLabel; } protected void grdOrgListData_PageIndexChanging(object sender, GridViewPageEventArgs e) { DataTable ds = new DataTable(); ds = (DataTable)Session["OrgDataSet"]; string strLabel = "Showing 0 to 0 of 0 records"; if (ds != null) { grdOrgListData.PageIndex = e.NewPageIndex; grdOrgListData.DataSource = ds; grdOrgListData.DataBind(); if (ds.Rows.Count > 0) { int intFrom = (e.NewPageIndex * 10) + 1; int intTo = (e.NewPageIndex * 10) + 10; if (intFrom == ds.Rows.Count) strLabel = "Showing " + intFrom + " to " + ds.Rows.Count + " of " + ds.Rows.Count + " records"; else if (intFrom < ds.Rows.Count && intTo >= ds.Rows.Count) strLabel = "Showing " + intFrom + " to " + ds.Rows.Count + " of " + ds.Rows.Count + " records"; else if (intFrom < ds.Rows.Count && intTo < ds.Rows.Count) strLabel = "Showing " + intFrom + " to " + intTo + " of " + ds.Rows.Count + " records"; } } else { grdOrgListData.PageIndex = e.NewPageIndex; grdOrgListData.DataSource = null; grdOrgListData.DataBind(); } lblRecordStatus.Text = strLabel; } protected void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e) { int intUserRole = Convert.ToInt32(Session["UserRole"]); if ((intUserRole == 0) || (intUserRole == 1)) { if (e.Row.RowType == DataControlRowType.DataRow) { ImageButton PlaceHolderView1 = e.Row.FindControl("imgBtnView") as ImageButton; PlaceHolderView1.ImageUrl = "../images/system/View_B.png"; ImageButton PlaceHolderView2 = e.Row.FindControl("imgBtnEdit") as ImageButton; PlaceHolderView2.ImageUrl = "../images/system/Edit_B.png"; MessageBox.Show("e.Row.Cells[] = " + e.Row.Cells[0].Text + " ,= " + e.Row.Cells[1].Text + " ,= " + e.Row.Cells[2].Text + " ,= " + e.Row.Cells[10].Text); if (Convert.ToInt32(e.Row.Cells[10].Text) == 0) { ImageButton PlaceHolderView3 = e.Row.FindControl("imgBtnDelete") as ImageButton; PlaceHolderView3.ImageUrl = "../images/system/Delete_G.png"; PlaceHolderView3.Enabled = false; ImageButton PlaceHolderView4 = e.Row.FindControl("imgBtnActive") as ImageButton; PlaceHolderView4.ImageUrl = "../images/system/Play_B.png"; } else if (Convert.ToInt32(e.Row.Cells[10].Text) == 1) { ImageButton PlaceHolderView3 = e.Row.FindControl("imgBtnDelete") as ImageButton; PlaceHolderView3.ImageUrl = "../images/system/Delete_B.png"; ImageButton PlaceHolderView4 = e.Row.FindControl("imgBtnActive") as ImageButton; PlaceHolderView4.ImageUrl = "../images/system/Play_G.png"; PlaceHolderView4.Enabled = false; } else if (Convert.ToInt32(e.Row.Cells[10].Text) == 2) { ImageButton PlaceHolderView3 = e.Row.FindControl("imgBtnDelete") as ImageButton; PlaceHolderView3.ImageUrl = "../images/system/Delete_G.png"; PlaceHolderView3.Enabled = false; ImageButton PlaceHolderView4 = e.Row.FindControl("imgBtnActive") as ImageButton; PlaceHolderView4.ImageUrl = "../images/system/Play_G.png"; PlaceHolderView4.Enabled = false; } } } } protected void imgBtnView_Click(object sender, ImageClickEventArgs e) { ImageButton btndetails = sender as ImageButton; GridViewRow gvrow = (GridViewRow)btndetails.NamingContainer; lblOrgID.Text = gvrow.Cells[0].Text; lblOrgName.Text = gvrow.Cells[1].Text; lblOrgDescription.Text = gvrow.Cells[2].Text; lblOrgAddress.Text = gvrow.Cells[3].Text; lblOrgZipCode.Text = gvrow.Cells[4].Text; lblOrgCountry.Text = gvrow.Cells[5].Text; lblOrgEmail.Text = gvrow.Cells[6].Text; lblOrgPhoneNo.Text = gvrow.Cells[7].Text; lblOrgWebsite.Text = gvrow.Cells[8].Text; lblOwnerEmail.Text = gvrow.Cells[9].Text; lblActiveStatus.Text = gvrow.Cells[10].Text; this.ModalPopupExtender1.Show(); } //Whether a particular data needs to be deleted or not protected void imgBtnDelete_Click(object sender, ImageClickEventArgs e) { ImageButton btndetails = sender as ImageButton; GridViewRow gvrow = (GridViewRow)btndetails.NamingContainer; Session["OrgDeleteRecord"] = gvrow.Cells[0].Text; lblOrgDelete.Text = gvrow.Cells[1].Text; this.ModalPopupExtender2.Show(); } //Clicked OK to delete a record from the DB and Grid protected void ButtonOk_Click(object sender, EventArgs e) { string strDeleteRecord = (string)Session["OrgDeleteRecord"]; int intOrgRowIndex = (int)Session["OrgRowIndex"]; DataTable dt = new DataTable(); dt = (DataTable)Session["OrgDataSet"]; DataRow[] rows; rows = dt.Select("OrgID='" + strDeleteRecord + "'"); foreach (DataRow row in rows) dt.Rows.Remove(row); grdOrgListData.DataSource = dt; grdOrgListData.DataBind(); Session["OrgDataSet"] = dt; } protected void imgBtnEdit_Click(object sender, ImageClickEventArgs e) { } protected void imgBtnActive_Click(object sender, EventArgs e) { } } } 

       protected void ButtonOk_Click(object sender, EventArgs e)
    {
        string strDeleteRecord = (string)Session["OrgDeleteRecord"];
        int intOrgRowIndex = (int)Session["OrgRowIndex"];
        DataTable dt = new DataTable();
        dt = (DataTable)Session["OrgDataSet"];

        DataRow[] rows;
        rows =  dt.Select("OrgID='" + strDeleteRecord + "'");
        foreach (DataRow row in rows)
            dt.Rows.Remove(row);

        //here you can call your LoadGridData() After Delete;

        this.LoadGridData();

        Session["OrgDataSet"] = dt;
    }

This error was because I made some of the columns in GridView invisible. First I made visible; then data binded; then made invisible again. That is working perfectly.

        //Making all columns in GridView visible to rebind the data 
        //Otherwise DataBind() will not work as there is checking at ActiveStatus[].
        for (int i = 0; i < 11; i++)
            grdOrgListData.Columns[i].Visible = true;

        grdOrgListData.DataSource = ds;
        grdOrgListData.DataBind();

        grdOrgListData.Columns[0].Visible = false;
        grdOrgListData.Columns[2].Visible = false;
        grdOrgListData.Columns[3].Visible = false;
        grdOrgListData.Columns[4].Visible = false;
        grdOrgListData.Columns[7].Visible = false;
        grdOrgListData.Columns[9].Visible = false;
        grdOrgListData.Columns[10].Visible = false;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM