hi i couldnt able to delete record from datagrid.i hve provide 2
checkbox one for selecting all records and another for selecting
single records and i provided paging also.without paging i can
possible to delete records.please help me out very i'm getting
problem.
here is my code
<asp:DataGrid ID="myGrid" runat="server" CellPadding="4"
AutoGenerateColumns="False"
            OnItemDataBound="dtgCustomers_ItemDataBound"
            OnPageIndexChanged="Page_indexChanged"
            PageSize="2"

                DataKeyField="fid" AllowPaging="True">
               <HeaderStyle CssClass="headerstyle" />
               <ItemStyle CssClass="item" />
           <Columns>
                    <asp:TemplateColumn>
                        <HeaderTemplate>
                            <input id="cbAll" runat="server"
onclick="setAll ( this )" type="checkbox" visible="true" />Select the
File to Delete
                            <input id="Button1" runat="server"
name="btndelete" onserverclick="deleteItems" type="button"
                                value="Delete" />
                        </HeaderTemplate>
                        <ItemTemplate>
                            <input id="cbItem" runat="server"
onclick="setProps ( this )" type="checkbox" value='<%#
myGrid.Items.Count %>'>
                            <%# ( ( DataRowView ) Container.DataItem )
[ "description" ] %>
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:HyperLinkColumn DataNavigateUrlField="fid"
DataNavigateUrlFormatString="download.aspx?fid={0}"
                        DataTextField="filename"
HeaderText="Filename"></asp:HyperLinkColumn>
                </Columns>
                <PagerStyle NextPageText="Next" PrevPageText="Prev" />
            </asp:DataGrid>

void Page_Load(Object src, EventArgs e)
    {
        if (!IsPostBack)
        {
            string query = "SELECT * from bookimage";
            DataTable myTable = (fetchData(query)).Tables[0];
            DataColumn[] keys = new DataColumn[1];
            keys[0] = myTable.Columns["BookImageid"];
            myTable.PrimaryKey = keys;
            Session["myTable"] = myTable;
            myGrid.DataSource = Session["myTable"];
            myGrid.DataBind();
            msg.InnerText = "Current record count is " +
myGrid.Items.Count;
        }
    }

    public void deleteItems(Object src, EventArgs e)
    {
        DataTable myTable = (DataTable)Session["myTable"];
        int deleted = 0, knt = myGrid.Items.Count;
        if (((HtmlInputCheckBox)myGrid.Controls[0].Controls
[0].FindControl("cbAll")).Checked)
        {
            myTable.Clear();
            deleted = knt;
        }
        else
        {

            DataRowCollection myRows = myTable.Rows;
            foreach (DataGridItem item in myGrid.Items)
            {
                HtmlInputCheckBox cbItem = (HtmlInputCheckBox)
item.FindControl("cbItem");
                if (cbItem.Checked)
                {
                     string supplierID = myGrid.DataKeys
[item.ItemIndex].ToString(); ;
                     DataRow thisRow = myRows.Find(supplierID);
                     if (thisRow != null)
                    {
                        string strdel = "Delete from bookimage where
BookImageid = " + supplierID + " ";
                        Response.Write(strdel);
                        SqlConnection con = new SqlConnection
(ConfigurationManager.AppSettings["example"]);
                        SqlCommand cmd = new SqlCommand(strdel, con);
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                        myRows.Remove(thisRow);

                    }

                }

                deleted++;
            }

        }
        Session["myTable"] = myTable;
        myGrid.DataSource = Session["myTable"];
        myGrid.DataBind();
        myGrid.Visible = (myGrid.Items.Count > 0);

    }
    protected void Page(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
        myGrid.CurrentPageIndex = e.NewPageIndex;
        BindDataGrid();
    }
    private void BindDataGrid()
    {
        string query = "Select * from bookimage ";
        SqlConnection con = new SqlConnection
(ConfigurationManager.AppSettings["example"]);
        SqlDataAdapter dr = new SqlDataAdapter(query, con);
        DataSet ds = new DataSet();
        dr.Fill(ds);
        myGrid.DataSource = ds;
        myGrid.DataBind();
    }
    DataSet fetchData(string query)
    {

        SqlConnection myConn = new SqlConnection
(ConfigurationSettings.AppSettings["example"]);
        SqlDataAdapter myAdapter = new SqlDataAdapter(query, myConn);
        DataSet myData = new DataSet();
        myAdapter.Fill(myData);
        return myData;
    }

Reply via email to