Gridview, multiple pages, custom checkbox within rows

Last post 10-22-2007 1:06 PM by Charles Asbornsen. 7 replies.

Sort Posts:

  • Gridview, multiple pages, custom checkbox within rows

    10-20-2007, 7:33 AM
    • Loading...
    • filipczyk
    • Joined on 08-08-2007, 4:48 PM
    • Posts 33
      

     Hi all again, i have a bit of a problem in this matter, hope that anyone can point me in the right direction, the scenario its as follows:

    I have a gridview, with about 200 rows, so i decided to page it, 10 rows per page; on each row i have a checkbox(not a gridview checkbox field, but a checkbox inside a template field, i was unhable to use the gridview checkbox´s field cause it was read only and y need to edit it in runtime). My problem comes when i want to set the checkbox to checked and the record its not on the first page, here´s some of my code:

     protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView2.PageIndex = e.NewPageIndex;
            SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["LocalSQLServer"]);
            SqlDataAdapter da = new SqlDataAdapter();
            SqlCommand Cmnd = new SqlCommand();
            Cmnd.CommandText = "SELECT * FROM Customers";
            Cmnd.Connection = conn;
    
            conn.Open();
            da.SelectCommand = Cmnd;
            da.SelectCommand.ExecuteNonQuery();
            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView2.DataSource = ds;
            GridView2.DataBind();
            for (int i = GridView2.PageIndex * 10; i < (GridView2.PageIndex * 10) + 9; i++)
            {
                DataRow dtRow = ds.Tables[0].Rows[i];
    
                if (bool.Parse(dtRow["BitField"].ToString()) == true)
                {
                    GridViewRow row = GridView2.Rows[i];
                    ((CheckBox)row.FindControl("CheckBox")).Checked = true;
    
                }
            }
            conn.Close();
            
        }
    

    My problem comes when i try to create the GridViewRow object row, cause the i in  GridView2.Rows[i], has a maximun value equal to the amount of records PER PAGE, with that in mind i tryed the above code, BUT, i still dont know how to select a specific page in order to set the correspondig checkbox(a page different form the 1st one), cause the i will only go as far as 9(only reference the rows on the 1st page).

    Any Ideas? 

    I hope i have make myself clear enough on this one, again pardon for my english and tnks in advance for the help.

     

    Farewell

     

    Leandro 

     

     

  • Re: Gridview, multiple pages, custom checkbox within rows

    10-20-2007, 8:20 AM

    Whats the point of changing a checkbox that isn't being displayed?  Maybe you need to move your logic into the checkbox's PreRender event.  Let the boxes themselves determine whether they should be checked after the databind. 

    May all your posts be enlightening...
    Charlie Asbornsen
    Dont forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: Gridview, multiple pages, custom checkbox within rows

    10-21-2007, 8:07 AM
    • Loading...
    • filipczyk
    • Joined on 08-08-2007, 4:48 PM
    • Posts 33

     Hi Charles, tnks for the reply, but im a bit lost with it, i´ve never used prerender event,(and specially not with checkboxes), can you clarify it a bit please?. I have a single checkbox within a gridview row template; how would i know in what row im in in order to, according to that, check the checkbox?, ask for datakey from gridview, do a DB access on it, and then change the state?. Wont it make ALL the checkboxes in ALL the rows have the same value?, how do i manage individually each checkbox, if each one has the same name?

    I´ll be more than gratefull if you can give me some more pointers.

    Tank you very much for the reply, and tnks in advance for the answer to this.

     

    Farewell

     

    Leandro. 

  • Re: Gridview, multiple pages, custom checkbox within rows

    10-21-2007, 3:38 PM

    There's a .Net concept known as reflection (at least I never heard of it until I started with .Net, who knows maybe its from FORTRAN or something).

    I'll have to check back tomorrow when I can look up the code in my office.  I'll give you something you can use.

    May all your posts be enlightening...
    Charlie Asbornsen
    Dont forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: Gridview, multiple pages, custom checkbox within rows

    10-22-2007, 8:34 AM
    • Loading...
    • filipczyk
    • Joined on 08-08-2007, 4:48 PM
    • Posts 33

    Ok Charles, i´ll be more than gratefull, in the meantime ill google a bit on that matter.

     

    Tnks again.

     

    Leandro 

  • Re: Gridview, multiple pages, custom checkbox within rows

    10-22-2007, 8:39 AM
    Answer

    Here's an example for you. You'll have to build a boolean array indexed to the DataItemIndexes from the first GV that stores the Checked property for the child GV based on whatever logic you are using.  Build that array after the first GV is databound but before the chiled GV is rendered.  BTW, this will only work if the same field is the DataItemIndex for both GVs.  You may have to get fancy with building the array if they are different (basically whatever column is used to maintain the relationship between the two grids is what you have to build your array on.

            protected virtual void cbxChildGVCheckbox_PreRender(object sender, EventArgs e)
            {
                string _dataItemRendered = ((GridViewRow)(((DataControlFieldCell)(((CheckBox)sender).Parent)).Parent)).DataItemIndex.ToString();
                if (ArrayShouldBeChecked[_dataItemRendered] == true)
                {
                    ((Checkbox)sender).Checked = true;
                }
                else
                {
                    ((Checkbox)sender).Checked = false;
                }
            }
     I hope this is helpful.
    
     
    May all your posts be enlightening...
    Charlie Asbornsen
    Dont forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: Gridview, multiple pages, custom checkbox within rows

    10-22-2007, 10:53 AM
    • Loading...
    • filipczyk
    • Joined on 08-08-2007, 4:48 PM
    • Posts 33

      Hi againCharles and thank you so much for the code, sadly, i would lie if i say i undesrtood it completely(i dont have 2 GVs, only one, and the checkbox its inside a column template), BUT, it gave me some ideas and i was able to fidure it out!, heres my solution:

     

    protected void CheckBox2_PreRender(object sender, EventArgs e)
        {
            string _dataItemRendered = ((GridViewRow)(((DataControlFieldCell)(((CheckBox)sender).Parent)).Parent)).DataItemIndex.ToString();
            int indice = int.Parse(_dataItemRendered) - (GridView2.PageIndex) * GridView2.PageSize;
            GridViewRow row = GridView2.Rows[indice];
            int Customer = int.Parse(row.Cells[0].Text.ToString());
            SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["LocalSQLServer"]);
            SqlDataAdapter da = new SqlDataAdapter();
            SqlCommand Cmnd = new SqlCommand();
            Cmnd.CommandText = "Select Selected from Customers where Customer=@Customer";
            Cmnd.Parameters.AddWithValue("@Customer",Customer);
            Cmnd.Connection = conn;
            conn.Open();
            bool Selected=bool.Parse(Cmnd.ExecuteScalar().ToString());
            conn.Close();
           
            if (Selected== true)
            {
                ((CheckBox)row.FindControl("CheckBox2")).Checked = true;
            }
            
    
        }

      I use the_dataItemRendered as a "Master Index" to figure out in wich row im in, with that and a bit of maths, im able to figure out mi current row; wich solves my problem.

    Thanks a millon again for the help Charles.

     

    Farewell

     

    Leandro 

  • Re: Gridview, multiple pages, custom checkbox within rows

    10-22-2007, 1:06 PM

    Oh sorry, when I saw GridView2 I assumed it was a child GV.

    May all your posts be enlightening...
    Charlie Asbornsen
    Dont forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
Page 1 of 1 (8 items)
Microsoft Communities
Page view counter