无法在datagridview中选择第一行中的复选框 [英] Checkbox in first row is not able to be selected in datagridview

查看:65
本文介绍了无法在datagridview中选择第一行中的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了选中所有复选框,如果我在头行上选中一个复选框但是没有选中第一个复选框。

我不知道我错过了什么。

这是Windows应用程序。

有人可以帮忙吗?

谢谢



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

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

private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
       {
           dataGridView1.ClearSelection();
       }

推荐答案

而不是服务器端,你可以使用javascript在客户端尝试相同的操作。 />


在你的aspx / ascx页面中添加以下javascript

< script language =javascripttype =text / javascript>

函数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 =='复选框'){

if(re.test(elm.name)&&!elm.disabled){

elm.checked = checkVal;

}

}

}

}

< / script>



复选框应添加到网格中,如下所示

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: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>


Hwllo,

这里有一篇非常好的文章:

切换DataGridView列内所有CheckBox的状态

谢谢
Hwllo ,
A very good article found here :
Toggling the States of all CheckBoxes Inside a DataGridView Column
Thanks


这篇关于无法在datagridview中选择第一行中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆