locked
Add datafield on checkbox using datagrid view's dataset RRS feed

  • Question

  • User-1575149674 posted

    hello Asp Users,

    how do you do it? Smile

    heres my back end code.

    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.SqlClient;
    using System.Configuration;
    
    namespace JIT
    {
    public partial class List : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    ResultAll();
    }
    protected void ResultAll()
    {
    string stxt = string.Empty;
    string conn = ConfigurationManager.ConnectionStrings["JIT"].ConnectionString;
    SqlConnection con = new SqlConnection(conn);
    con.Open();
    
    stxt = "select * from acc";
    SqlCommand cmd = new SqlCommand(stxt, con);
    SqlDataAdapter adptr = new SqlDataAdapter();
    adptr.SelectCommand = cmd;
    
    DataSet dst = new DataSet();
    adptr.Fill(dst);
    
    
    if (dst.Tables.Count > 0 && dst.Tables[0].Rows.Count > 0)
    {
    
    GrdView.DataSource = dst;
    GrdView.DataBind();
    }
    con.Close();
    }
    }
    }

    and heres my front end

        <asp:GridView ID="GrdView" runat="server" AutoGenerateColumns="false">
                <Columns>
                    <asp:CheckBoxField DataField="ID" />    
                    </Columns>
            </asp:GridView>
        </div>

    i search the internet but i cant find anything.

    thanks for help, 

    Sunday, June 8, 2014 9:18 PM

Answers

  • User-734925760 posted

    CheckBox Checkbox1 = (CheckBox)row.FindControl("CheckBox1");
          int id = Convert.ToInt32(GridView.DataKeys[row.RowIndex].Value);
                        if(Checkbox1.Checked == true){
    
                        }
    

    Hi,

    According to your description and the code you provided, in every Gridview row, you will have a checkbox control, so as we click the delete button control, we should loop all the rows then check which rows need to be deleted, please refer to the code below:

    foreach(GridViewRow gvrow in GrdView.Rows)
    {
     CheckBox chkdelete=(CheckBox )GrdView.FindControl("CheckBox1");
    if(chkdelete.Checked)
    {
    }
    }

    There is an artical about your requirement, please refer to the link below:

    http://www.codecomplete4u.com/delete-multiple-rows-gridview-using-checkbox-asp-net/

    Hope it's useful for you.

    Best Regards,

    Michelle Ge

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 10, 2014 3:28 AM
  • User-1575149674 posted

    Hi Guys!

    At last i already did it,

    here's how i did it

    main 

     protected void Page_Load(object sender, EventArgs e)
            {
                if(!IsPostBack){
                ResultAll();
                }
                ye.Text = "";
                }
            ......

    to control the check you need to put IsPostBack that should do the trick,

    submit code:

          int i=0;
                foreach (GridViewRow row in GrdView.Rows)
                {
                   CheckBox Checkbox1 = (CheckBox)row.FindControl("CheckBox1");
                  if (Checkbox1.Checked)
                    {
                        ye.Text += Convert.ToInt32(GrdView.DataKeys[row.RowIndex].Values["ID"]);
                    }
                    
                }


    i added the ye.txt temporarily just to check if it could handle the multiple checks..

    thanks .net Pros! wish you the best 

    Cheers!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 13, 2014 5:18 AM

All replies

  • User-1716253493 posted

    CheckBox checked property should bound to boolen value from database

    Example for that is active/inactive value

    Create column called Active with Bit datatype

        <asp:GridView ID="GrdView" runat="server" AutoGenerateColumns="false">
                <Columns>
                    <asp:CheckBoxField DataField="Active" />    
                </Columns>
         </asp:GridView>

    Sunday, June 8, 2014 9:29 PM
  • User-1575149674 posted

    Hi what im planning to do is create a gridview that has delete button then once i check a multiple checkboxes click the delete button it will delete the selected row.. i need to add the ID(databse primary key column) on the datafield so that i can send it on sql query and delete it. anyway is it possible using the boolean you propose? or what function do i need? can you please show me how?

    Sunday, June 8, 2014 10:31 PM
  • User-1716253493 posted

    Place checkbox control to gridview templatefield itemtemplate.

    Set Gridview DataKeyNames = "id"

    Loop the grid

            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox CheckBox1 = (CheckBox)row.FindControl("CheckBox1");
                int id = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
                if (CheckBox1.Checked == true)
                {
                    //delete here
                }
            }

    Sunday, June 8, 2014 11:13 PM
  • User-1575149674 posted

    sup!

    sorry for the late reply

    here's the frontend

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="List.aspx.cs" Inherits="JIT.List" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            
            <asp:GridView ID="GrdView" runat="server" AutoGenerateColumns="false" DataKeyNames="ID">
               <Columns>
                   <asp:TemplateField>
                       <ItemTemplate>
                           <asp:CheckBox runat="server" ID="CheckBox1"/>
                       </ItemTemplate>
                        </asp:TemplateField>
               </Columns>
            </asp:GridView>
            <br />
        </div>
        </form>
    </body>
    </html>
    

    i put ID on the DatakeyNames because thats the name of the datafield and it came from the Database


    here's the codebehind
    added this

    foreach (GridViewRow row in GrdView.Rows)
                    {
                        CheckBox Checkbox1 = (CheckBox)row.FindControl("CheckBox1");
                        int id = Convert.ToInt32(GridView.DataKeys[row.RowIndex].Value);
                        if(Checkbox1.Checked == true){
    
                        }
                    }
    

    found error an error 

    Error 1 An object reference is required for the non-static field, method, or property 'System.Web.UI.WebControls.GridView.DataKeys.get'
    sorry to keep on questioning i'm still adjusting on .net's environment



    ALREADY FOUND THE BUG thx 

    Monday, June 9, 2014 5:07 AM
  • User-734925760 posted

    CheckBox Checkbox1 = (CheckBox)row.FindControl("CheckBox1");
          int id = Convert.ToInt32(GridView.DataKeys[row.RowIndex].Value);
                        if(Checkbox1.Checked == true){
    
                        }
    

    Hi,

    According to your description and the code you provided, in every Gridview row, you will have a checkbox control, so as we click the delete button control, we should loop all the rows then check which rows need to be deleted, please refer to the code below:

    foreach(GridViewRow gvrow in GrdView.Rows)
    {
     CheckBox chkdelete=(CheckBox )GrdView.FindControl("CheckBox1");
    if(chkdelete.Checked)
    {
    }
    }

    There is an artical about your requirement, please refer to the link below:

    http://www.codecomplete4u.com/delete-multiple-rows-gridview-using-checkbox-asp-net/

    Hope it's useful for you.

    Best Regards,

    Michelle Ge

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 10, 2014 3:28 AM
  • User-1575149674 posted

    Hi Michelle sorry for the late reply, i'll check this 

    thanks!

    Friday, June 13, 2014 2:41 AM
  • User-1575149674 posted

    Hi Guys!

    At last i already did it,

    here's how i did it

    main 

     protected void Page_Load(object sender, EventArgs e)
            {
                if(!IsPostBack){
                ResultAll();
                }
                ye.Text = "";
                }
            ......

    to control the check you need to put IsPostBack that should do the trick,

    submit code:

          int i=0;
                foreach (GridViewRow row in GrdView.Rows)
                {
                   CheckBox Checkbox1 = (CheckBox)row.FindControl("CheckBox1");
                  if (Checkbox1.Checked)
                    {
                        ye.Text += Convert.ToInt32(GrdView.DataKeys[row.RowIndex].Values["ID"]);
                    }
                    
                }


    i added the ye.txt temporarily just to check if it could handle the multiple checks..

    thanks .net Pros! wish you the best 

    Cheers!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 13, 2014 5:18 AM