欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#中DataGridView常用操作实例小结

程序员文章站 2022-10-15 10:21:59
本文实例讲述了c#中datagridview常用操作。分享给大家供大家参考。具体如下: public void binder1() { datatable...

本文实例讲述了c#中datagridview常用操作。分享给大家供大家参考。具体如下:

public void binder1()
{
 datatable tabletype = database.sqldbhelper.getdatatable("select top 200 unit_code,unit_name from unit ");
 datatable table = database.sqldbhelper.getdatatable("select top 2 * from tempproduct");
 datagridviewrow dgvr;
 foreach (datarow row in table.rows)
 {
  dgvr = new datagridviewrow();
  dgvr.createcells(datagridview);
  dgvr.cells[0].value = row["id"].tostring();
  dgvr.cells[1].value = row["name"].tostring();
  dgvr.cells[2].value = row["age"].tostring();
  dgvr.cells[3].value = row["address"].tostring();
  //绑定下拉列表
  datagridviewcomboboxcolumn dgvcbc = datagridview.columns[4] as datagridviewcomboboxcolumn;
  if (dgvcbc != null)
  {
   //绑定下来列表
   dgvcbc.datasource = tabletype;
   dgvcbc.displaymember = "unit_name";
   dgvcbc.valuemember = "unit_code";
  }
  //为下拉列表设置默认值
  dgvr.cells[4].value = row["entryid"].tostring();
  //设置复选框是否选中
  dgvr.cells[5].value = row["flag"].tostring() == "0" ? true : false;
  //在列表中找到datagridviewlinkcolumn
  datagridviewlinkcolumn links = datagridview.columns[6] as datagridviewlinkcolumn;
  if (links != null)
  {
   //需要设置datagridviewlinkcolumn的usecolumntextforlinkvalue属性为true才会有作用
   links.text = "点击查看";
  }
  //在列表中找到datagridviewbuttoncolumn
  datagridviewbuttoncolumn button = datagridview.columns[7] as datagridviewbuttoncolumn;
  if (button != null)
  {
   //需要设置datagridviewbuttoncolumn的usecolumntextforlinkvalue属性为true才会有作用
   button.text = "点击查看";
  }
  datagridview.rows.add(dgvr);
 }
}

希望本文所述对大家的c#程序设计有所帮助。