ASP.NET - check checkbox - Asked By msakt on 08-Jun-12 05:34 AM

dataset have more data like id name add phone email etc.....

for example ds have 1 to 1000 records


i have some id ......1,5,7,4,9   this particular id records is checked in checkbox inside gridview ...


grdiview design with checkbox.....
Chintan Vaghela replied to msakt on 08-Jun-12 06:01 AM

Hi Frndz,

 

Functionality:  Checked Gridview check box row condition based

 

Make one DataTable , bind  db table value into datatable

 

After then check condition in sql reader to read each value

 

 

if (Convert.ToInt32(rdr[0]) == 1 || Convert.ToInt32(rdr[0]) == 3 || Convert.ToInt32(rdr[0]) == 7)

            {

              dr["chkvalue"] = true;

            }

            else

            {

              dr["chkvalue"] = false;

            }

 

 

Assign this value to item template checkbox control

 

          <ItemTemplate>

            <asp:CheckBox ID="chkList" Checked='<%# Eval("chkvalue") %>' runat="server" />

          </ItemTemplate>

 

 

 

Logic::

 

Bind Grid

  grdAccDetails.DataSource = DynamcDataTable();

        grdAccDetails.DataBind();

 

Gridview aspx page with checkbox

 

<asp:GridView ID="grdAccDetails" runat="server" AutoGenerateColumns="False"

>

      <Columns>

        <asp:TemplateField HeaderText="test">

          <ItemTemplate>

            <asp:CheckBox ID="chkList" Checked='<%# Eval("chkvalue") %>' runat="server" />

          </ItemTemplate>

        </asp:TemplateField>

        <asp:BoundField DataField="id" HeaderText="id" SortExpression="id" />

       

      </Columns>

    </asp:GridView>

 

 

 

Bind Datatable

private DataTable DynamcDataTable()

    {

      DataTable dt = new DataTable();

      using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDbConn"].ToString()))

      {

 

        DataRow dr = null;

        dt.Columns.Add(new DataColumn("chkvalue", typeof(Boolean)));

        dt.Columns.Add(new DataColumn("id", typeof(string)));

        dt.Columns.Add(new DataColumn("name", typeof(string)));

        SqlCommand cmd = new SqlCommand("select id,name,address from tablename", cn);

        cn.Open();

        SqlDataReader rdr = cmd.ExecuteReader();

        if (rdr.HasRows)

        {

          while (rdr.Read())

          {

            dr = dt.NewRow();

            if (Convert.ToInt32(rdr[0]) == 1 || Convert.ToInt32(rdr[0]) == 3 || Convert.ToInt32(rdr[0]) == 7)

            {

              dr["chkvalue"] = true;

            }

            else

            {

              dr["chkvalue"] = false;

            }

            dr["id"] = rdr[1].ToString();

            dr["name"] = rdr[1].ToString();

            dt.Rows.Add(dr);

          }

        }

        cn.Close();

      }

 

      return dt;

 

    }

 

Hope this helpful!

Thanks