Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3275668
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:10:12+00:00 2026-05-17T19:10:12+00:00

How to add the checkbox field in gridview programatically, what’s wrong with my code?

  • 0

How to add the checkbox field in gridview programatically, what’s wrong with my code?

try
{
  string Data_source=@"Data Source=A-63A9D4D7E7834\SECOND;";
  string Initial_Catalog=@"Initial Catalog=replicate;";
  string User=@"User ID=sa;";
  string Password=@"Password=two";
  string full_con=Data_source+Initial_Catalog+User+Password;
  SqlConnection connection = new SqlConnection(full_con);
  connection.Open();
  SqlCommand numberofrecords = new SqlCommand("SELECT COUNT(*) FROM dbo.Table_1", connection);
  DataSet ds2 = new DataSet();
  SqlDataAdapter testadaptor = new SqlDataAdapter();
  testadaptor.SelectCommand = new SqlCommand("SELECT COUNT(*) FROM dbo.Table_1", connection);
  testadaptor.Fill(ds2);
  grid1.DataSource = ds2;
  CheckBoxField c = new CheckBoxField();
  grid1.Columns.Add(c);
  grid1.DataBind();
  numberofrecords.Dispose();
  connection.Close();
  connection.Dispose();
}
catch (Exception a)
{
  Response.Write("Please check  ");
   Response.Write(a.Message.ToString());
   Response.Write(a.Source.ToString());
}//catch
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:10:13+00:00Added an answer on May 17, 2026 at 7:10 pm

    The CheckBoxField will probably want a value for the DataField property. This should match the column names or aliases in your query. (I don’t think a checkbox will work with number results, though.)

    Edited: didn’t realize what you were trying to do. A template field and a regular checkbox should get you closer to what you want. Something like this?

    e.g.

    const int ColumnSelect = 0;
    
    protected void Page_Load(object sender, EventArgs e)
    {       
        //Get real data here.
        DataTable dt = new DataTable();
        dt.Columns.Add("count");                
        dt.Rows.Add(dt.NewRow());
        dt.Rows[0][0] = "5";
    
        GridView1.Columns.Add(new TemplateField());        
        BoundField b = new BoundField();
        GridView1.Columns.Add(b);
        b.DataField = "count";
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    
    
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.Header)
        {            
            e.Row.Cells[ColumnSelect].Controls.Add(new CheckBox());
        }
    }
    

    Edit #2: as for getting the value, you can certainly do this. Are you looking for a Javascript or server-side solution? Here’s a simple example for server-side if you had a button click:

    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach(GridViewRow row in GridView1.Rows)
        {
            //Could also use (CheckBox)row.Cells[ColumnSelect].FindControl if you give the checkboxes IDs when generating them.
            CheckBox cb = (CheckBox)row.Cells[ColumnSelect].Controls[0];
    
            if (cb.Checked)
            {
                //Do something here.
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.