urgent : setting the checkbox programmatically in gridview

Last post 08-02-2007 1:29 PM by ecbruck. 5 replies.

Sort Posts:

  • urgent : setting the checkbox programmatically in gridview

    08-02-2007, 7:12 AM
    • Loading...
    • Kuruvilla
    • Joined on 06-26-2007, 8:16 AM
    • Posts 64

    How do i set the checkbox (template) of a gridview programmatically in the code view  

     this is the code pasted in the code behind:

    but it doesnt work .... 

    please help ... 

     int i = 0;
                    foreach (GridViewRow row in Grid_Reserve.Rows)
                    {
                        i = i + 1;
                        CheckBox checkbox = row.FindControl("CheckBox1") as CheckBox;
                        string str = " select  count(*) from Issue_Book_Reserve, Books  where  Issue_Book_Reserve.Book_Id = Books.Book_Id and Books.Book_Name =  '" + Grid_Issue.Rows[i].Cells[2].Text + "' and Issue_Book_Reserve.Issue_Book_Reserve_State = 3";
                        SqlCommand checking = new SqlCommand(str, con);
                        int count = (int)checking.ExecuteScalar();
                        if (count < 1)

                            Grid_Issue.Rows[i].Cells[0].Text = "true";
                        //                    checkbox.Checked = true; //in case i use this , a null pointer exception is used



                    }
                }

    Filed under: ,
  • Re: urgent : setting the checkbox programmatically in gridview

    08-02-2007, 9:20 AM
    • Loading...
    • ecbruck
    • Joined on 12-30-2005, 2:39 PM
    • Des Moines, IA
    • Posts 7,603
    • Moderator
      TrustedFriends-MVPs

    One thing I see right off the bat is the use of your counters. Get rid of it! During your first iteration, you'd be pointing to the row with an index of 1 which is actually the second row. You're already iterating through the rows, so simply use the "row" reference from your foreach statement for retrieving your references. Don't try to directly reference your row through the use of an index.

    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

    protected void Post_Answered(object sender, EventArgs e) { if (this.MarkAsAnswered != null) { this.MarkAsAnswered(this, EventArgs.Empty); } }
  • Re: urgent : setting the checkbox programmatically in gridview

    08-02-2007, 12:03 PM
    • Loading...
    • Kuruvilla
    • Joined on 06-26-2007, 8:16 AM
    • Posts 64

     ya i too found that to be true earlier ..but then that done how do i set the checkbox to be true?

    any clues.. 

  • Re: urgent : setting the checkbox programmatically in gridview

    08-02-2007, 12:10 PM
    • Loading...
    • ecbruck
    • Joined on 12-30-2005, 2:39 PM
    • Des Moines, IA
    • Posts 7,603
    • Moderator
      TrustedFriends-MVPs

    I'm not exactly sure what you're doing with the Sql query, but all you need to do is retrieve a reference to your CheckBox using row.FindControl and then set the Checked property from that reference.

    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

    protected void Post_Answered(object sender, EventArgs e) { if (this.MarkAsAnswered != null) { this.MarkAsAnswered(this, EventArgs.Empty); } }
  • Re: urgent : setting the checkbox programmatically in gridview

    08-02-2007, 1:26 PM
    • Loading...
    • Kuruvilla
    • Joined on 06-26-2007, 8:16 AM
    • Posts 64

    dont u mean like this? 

    CheckBox  checkbox  = Gridviewrow.findcontrol("checkbox1")

    checkbox.checked = true ? 

     

    in case its this i tried this , it doesnt work ..im missing something

  • Re: urgent : setting the checkbox programmatically in gridview

    08-02-2007, 1:29 PM
    Answer
    • Loading...
    • ecbruck
    • Joined on 12-30-2005, 2:39 PM
    • Des Moines, IA
    • Posts 7,603
    • Moderator
      TrustedFriends-MVPs

    Yes, except you need to cast the result of the FindControl method to a CheckBox.

    CheckBox checkbox = GridVwiewrow.FindControl("checkbox1") as CheckBox

    checkbox.Checked = true

    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

    protected void Post_Answered(object sender, EventArgs e) { if (this.MarkAsAnswered != null) { this.MarkAsAnswered(this, EventArgs.Empty); } }
Page 1 of 1 (6 items)
Microsoft Communities
Page view counter