Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have implemented to select all of checkboxes if I select a checkbox on the head row but only first checkbox in a row is not selected.
I don't know what I am missing.
It is windows application.
Can anybody help?
Thank you

C#
private void Form1_Load(object sender, EventArgs e)
       {
           First_Grid();
       }
       private void First_Grid()
       {
           conn = new SQLiteConnection(strConn);

           conn.Open();
           comm = new SQLiteCommand("select idx,Thumnail_url  From cars order by idx", conn);


           sda = new SQLiteDataAdapter(comm);

           ds = new DataSet();
           sda.Fill(ds, "cars");


           CalculateTotalPages();


           DataGridViewCheckBoxColumn CheckboxColumn = new DataGridViewCheckBoxColumn();
           CheckBox chk = new CheckBox();
           CheckboxColumn.Width = 20;
           CheckboxColumn.Name = "col_checkbox";

           dataGridView1.Columns.Add(CheckboxColumn);


           dataGridView1.Columns.Add("idx", "idx");
           var dvi = new DataGridViewImageColumn { HeaderText = "thumnail_url" };
           dataGridView1.Columns.Add(dvi);


           // int row = ds.Tables["cars"].Rows.Count - 1;
           int row = GetCurrentRecords(CurrentPageIndex, conn).Rows.Count - 1;
           for (int r = 0; r <= row; r++)
           {
               dataGridView1.Rows.Add();
               //  dataGridView1.Rows[r].Cells[0].Value = ds.Tables["cars"].Rows[r].ItemArray[0];
               dataGridView1.Rows[r].Cells[1].Value = GetCurrentRecords(CurrentPageIndex, conn).Rows[r].ItemArray[0];

               //to show image from url on column cells[3]
               System.Net.WebClient webSource = new System.Net.WebClient();
               //string url = ds.Tables["cars"].Rows[r].ItemArray[1].ToString();
               string url = GetCurrentRecords(CurrentPageIndex, conn).Rows[r].ItemArray[1].ToString();
               byte[] data = webSource.DownloadData(url);
               System.IO.MemoryStream pipe = new System.IO.MemoryStream(data);
               Image jpgImage = Image.FromStream(pipe);
               dataGridView1.Rows[r].Cells[2].Value = jpgImage;

           }
           lbl_Page.Text = "1/" + TotalPage.ToString();

       }
       private void Again_Grid(int PageIndex)
       {
           conn = new SQLiteConnection(strConn);

           conn.Open();
           comm = new SQLiteCommand("select idx,Thumnail_url  From cars order by idx", conn);


           sda = new SQLiteDataAdapter(comm);

           ds.Clear();
           ds = new DataSet();
           sda.Fill(ds, "cars");


           CalculateTotalPages();


           // int row = ds.Tables["cars"].Rows.Count - 1;
           int row = GetCurrentRecords(CurrentPageIndex, conn).Rows.Count - 1;
           for (int r = 0; r <= row; r++)
           {
               //  dataGridView1.Rows[r].Cells[0].Value = ds.Tables["cars"].Rows[r].ItemArray[0];
               dataGridView1.Rows[r].Cells[1].Value = GetCurrentRecords(CurrentPageIndex, conn).Rows[r].ItemArray[0];

               //to show image from url on column cells[3]
               System.Net.WebClient webSource = new System.Net.WebClient();
               //string url = ds.Tables["cars"].Rows[r].ItemArray[1].ToString();
               string url = GetCurrentRecords(CurrentPageIndex, conn).Rows[r].ItemArray[1].ToString();
               byte[] data = webSource.DownloadData(url);
               System.IO.MemoryStream pipe = new System.IO.MemoryStream(data);
               Image jpgImage = Image.FromStream(pipe);
               dataGridView1.Rows[r].Cells[2].Value = jpgImage;

           }
           lbl_Page.Text = CurrentPageIndex.ToString() + "/" + TotalPage.ToString();


       }
       private DataTable GetCurrentRecords(int page, SQLiteConnection conn)
       {
           dt = new DataTable();
           if (page == 1)
           {
               comm = new SQLiteCommand("Select idx,Thumnail_url from cars ORDER BY idx limit " + PageSize, conn);
           }
           else
           {
               int PreviouspageLimit = (page - 1) * PageSize;

               comm = new SQLiteCommand("Select " +
                   " idx,Thumnail_url  from cars " +
                   "where idx NOT IN " +
               "(Select idx from cars ORDER BY idx limit " + PreviouspageLimit + ") limit " + PageSize, conn); // +
               //"order by customerid", con);
           }
           try
           {
               // con.Open();
               sda.SelectCommand = comm;
               sda.Fill(dt);

           }
           finally
           {
               conn.Close();
           }
           return dt;
       }
       private void CalculateTotalPages()
       {
           int rowCount = ds.Tables["cars"].Rows.Count;
           TotalPage = rowCount / PageSize;
           if (rowCount % PageSize > 0) // if remainder is more than  zero
           {
               TotalPage += 1;
           }
       }


       private void gvSheetListCheckBox_CheckedChanged(object sender, EventArgs e)
       {
           foreach (DataGridViewRow r in dataGridView1.Rows)
           {
               r.Cells["col_checkbox"].Value = ((CheckBox)sender).Checked;
           }
       }

       private void dataGridView1_CellPainting_1(object sender, DataGridViewCellPaintingEventArgs e)
       {
           if (e.ColumnIndex == 0 && e.RowIndex == -1)
           {
               e.PaintBackground(e.ClipBounds, false);

               Point pt = e.CellBounds.Location;  // where you want the bitmap in the cell

               int nChkBoxWidth = 15;
               int nChkBoxHeight = 15;
               int offsetx = (e.CellBounds.Width - nChkBoxWidth) / 2;
               int offsety = (e.CellBounds.Height - nChkBoxHeight) / 2;

               pt.X += offsetx;
               pt.Y += offsety;

               CheckBox cb = new CheckBox();
               cb.Size = new Size(nChkBoxWidth, nChkBoxHeight);
               cb.Location = pt;
               cb.CheckedChanged += new EventHandler(gvSheetListCheckBox_CheckedChanged);

               ((DataGridView)sender).Controls.Add(cb);

               e.Handled = true;
           }
       }


What I have tried:

I have added this code but it didn't work
C#
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
       {
           dataGridView1.ClearSelection();
       }
Posted
Updated 12-May-16 23:37pm
v3

Instead of doing it is server side, u can try the same in client side using javascript.

Add the below javascript in you aspx/ascx page
<script language="javascript" type="text/javascript">
function CheckAllDataListCheckBoxes(checkBoxID, checkVal) {
var re = new RegExp(checkBoxID);
for (i = 0; i < document.forms[0].elements.length; i++) {
elm = document.forms[0].elements[i]
if (elm.type == 'checkbox') {
if (re.test(elm.name) && !elm.disabled) {
elm.checked = checkVal;
}
}
}
}
</script>

the checkbox should be added to grid as given below
ASP.NET
<asp:TemplateField>
              <HeaderTemplate>
                  <table>
                      <tr>
                          <td>
                              <asp:CheckBox ID="chkbxSelectAll" onclick="CheckAllDataListCheckBoxes('chkbxProject', this.checked)"
                                  runat="server" ToolTip="Select All" />
                          </td>
                      </tr>
                  </table>
              </HeaderTemplate>
              <ItemTemplate>
                  <asp:CheckBox ID="chkbxProject" runat="server" ToolTip='<%# DataBinder.Eval(Container.DataItem,"ProjectDescCodeDB") %>' />
                  <input type="hidden" id="hdnProjectId" runat="server" value='<%# DataBinder.Eval(Container.DataItem,"ID") %>' />
                  <input type="hidden" id="hdnIsGlobBid" runat="server" value='<%# DataBinder.Eval(Container.DataItem,"IsGlobalBid") %>' />
              </ItemTemplate>
          </asp:TemplateField>
 
Share this answer
 
Comments
hapiten 13-May-16 3:12am    
Thank you for your answer but it is windows application program.Sorry I haven't mentioned it
Hwllo ,
A very good article found here :
Toggling the States of all CheckBoxes Inside a DataGridView Column
Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900