Results 1 to 16 of 16

Thread: Checkbox in GridView

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2006
    Posts
    719

    Checkbox in GridView

    i use this code to delete records in datagridview

    Code:
            // Iterate through the Products.Rows property
            bool atLeastOneRowDeleted = false;
            foreach (GridViewRow row in grdMessages.Rows)
            {
                if (((CheckBox)row.FindControl("chkMessage")).Checked == true)
                {
                    atLeastOneRowDeleted = true;
                }
            }
    But I encounter this message;

    The varialbe 'atLeastOneRowDeleted' is assigned but its value is never used
    and the 'atLeastOneRowDeleted' return False result even if i check a checkbox on the grid

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Checkbox in GridView

    1.Where do you check the values.
    2.Do you check the checked property value before binding the gridview.
    3.Normally when the page is posted and the gridview is binded the checkbox lost its value
    Please mark you thread resolved using the Thread Tools as shown

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2006
    Posts
    719

    Re: Checkbox in GridView

    1.Where do you check the values.

    I check from checkbox inside datagridview
    2.Do you check the checked property value before binding the gridview.
    i check it after binding then put the code in a button
    3.Normally when the page is posted and the gridview is binded the checkbox lost its value
    so what should I do now?

  4. #4
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Checkbox in GridView

    3.Normally when the page is posted and the gridview is binded the checkbox lost its value[/QUOTE]



    Post the entire coding
    Please mark you thread resolved using the Thread Tools as shown

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2006
    Posts
    719

    Re: Checkbox in GridView

    Code:
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                BindData();
        }
    Code:
        private void BindData()
        {
            SqlConnection oConn = new SqlConnection(ConfigurationManager.AppSettings["constring"]);
    
    
            SqlDataAdapter cmdGrid = default(SqlDataAdapter);
            DataSet dsGrid = new DataSet();
            cmdGrid = new SqlDataAdapter("SELECT * FROM Messages Order By MessageSent DESC", oConn);
    
            cmdGrid.SelectCommand.CommandTimeout = 300;
    
            cmdGrid.Fill(dsGrid, "Messages");
    
            DataView dv = new DataView(dsGrid.Tables["Messages"]);
    
            grdMessages.DataSource = dv;   
            grdMessages.DataBind();
        }
    Code:
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            // Iterate through the Products.Rows property
            bool atLeastOneRowDeleted = false;
            foreach (GridViewRow row in grdMessages.Rows)
            {
                if (((CheckBox)row.FindControl("chkMessage")).Checked == true)
                {
                    atLeastOneRowDeleted = true;
                }
            }
        }

  6. #6
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: Checkbox in GridView

    can you post the aspx code, specially the id of checkbox...
    __________________
    Rate the posts that helped you

  7. #7
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Checkbox in GridView

    1.Have you placed the delete button on each row are separately ?
    2. Did you debug the code. Does it find the checkbox and its state ?

    foreach (GridViewRow row in grdMessages.Rows)
    {
    if (((CheckBox)row.FindControl("chkMessage")).Checked == true)
    {
    atLeastOneRowDeleted = true;
    }
    }
    Please mark you thread resolved using the Thread Tools as shown

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2006
    Posts
    719

    Re: Checkbox in GridView

    CheckBox ID is: chkMessage

    1. The delete button was out the datagridview.
    2. There is not bug.. it uncheck all the checkbox after i Clicked the delete button.

  9. #9
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Checkbox in GridView

    Hello,

    The error/warning that you are getting is because you never actually use the local variable that you are creating. Yes, you assign a value to it, but you never do anything with the value.

    Are you going to return that value to somewhere? Or what decision are you going to take based on it's value?

    Gary

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2006
    Posts
    719

    Re: Checkbox in GridView

    what i want to happen is when I check the checkbox and click the delete button.. the selected items will be deleted too..

  11. #11
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: Checkbox in GridView

    It will be more convinient to us if you can post the entire page code (including html markup code) as we can’t see any code that actually delete the records

    Also try by changing
    Code:
    row.FindControl("chkMessage")).Checked
    With
    Code:
    row.Cells[index_of_column_having_Checkbox].FindControl("chkMessage")
    __________________
    Rate the posts that helped you

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2006
    Posts
    719

    Re: Checkbox in GridView

    thanks, attached with this email are the aspx files..
    Attached Files Attached Files

  13. #13
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Checkbox in GridView

    Hello,

    The code that you have works perfectly, as long as you make one important change.

    In the PageLoad event, you are re-binding the GridView every time the pages loads. As a result, you are losing the changes that the user has made on the client, i.e. when they check the CheckBox. Rather than re-binding on every page load, you want to make sure that you are only binding when the page first loads.

    This can be achieved using the following check:

    Code:
                if (!this.IsPostBack)
                {
                    SqlConnection oConn = new SqlConnection(ConfigurationManager.AppSettings["constring"]);
    
                    .... <rest of your code goes here>
                }
    Hope that helps!

    Gary

  14. #14
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Checkbox in GridView

    Thanks Gary.

    That's what I was referring to in post #4
    Please mark you thread resolved using the Thread Tools as shown

  15. #15
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Checkbox in GridView

    Hey Dana,

    I thought that is what you were getting at. It was only when we saw the whole code that we got to see what the problem was.

    Gary

  16. #16
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Checkbox in GridView

    Quote Originally Posted by gep13 View Post
    Hey Dana,

    I thought that is what you were getting at. It was only when we saw the whole code that we got to see what the problem was.

    Gary
    Please mark you thread resolved using the Thread Tools as shown

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width