DataGrid相关知识总结(收集自各位老大处)[1]

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 jigee 的 blog

关于datagrid的问题,如何使行宽不可由用户更改。(即行宽固定,不能通过拖拉的方式改变)
定义datagrid的时候就把宽度设定
<asp:boundcolumn ...>                       <headerstyle width="150px"></headerstyle>

如何在winform中datagrid点击某行,使数据实时显示在textbox中?
datagrid的keypress事件中

textbox1.text=mydatagrid(mydatagrid.currentcell.rownumber,0)
textbox2.text=mydatagrid(mydatagrid.currentcell.rownumber,1)
........

以此类推

namespace datagriddoubleclick
{
 using system;
 using system.drawing;
 using system.collections;
 using system.componentmodel;
 using system.windows.forms;
 using system.data;

 public class form1 : system.windows.forms.form
 {
  private system.windows.forms.datagrid datagrid1;
  private dataset mydataset;
  datetime gridmousedowntime;
    private system.windows.forms.label label1;
  
  private system.componentmodel.container components = null;

  public form1()
  {
   initializecomponent();
   gridmousedowntime = datetime.now;
   setup();
  }

  private void setup()
  {
   // 用2个table和1和relation创建dataset
   makedataset();
   // 数据绑定
   datagrid1.setdatabinding(mydataset, "customers");

   //添加样式
   addcustomdatatablestyle();
  }

  private void makedataset()
  {
   // 创建dataset.
   mydataset = new dataset("mydataset");
      
   // 创建2个datatables.
   datatable tcust = new datatable("customers");
   
   // 创建两个列,并添加到第一个表
   datacolumn ccustid = new datacolumn("custid");
   datacolumn ccustname = new datacolumn("custname");
   datacolumn ccurrent = new datacolumn("custcity");
   tcust.columns.add(ccustid);
   tcust.columns.add(ccustname);
   tcust.columns.add(ccurrent);

   // 把tables添加到dataset.
   mydataset.tables.add(tcust);
   
   
   /* 计算tables.对每个客户,创建datarow变量 */
   datarow newrow1;
   
   // 添加记录到 customers table.
   for(int i = 1; i < 4; i++)
   {
    newrow1 = tcust.newrow();
    newrow1["custid"] = (100*i).tostring();
    tcust.rows.add(newrow1);
   }

   tcust.rows[0]["custname"] = "【孟宪会之精彩世界】";
   tcust.rows[1]["custname"] = "net_lover";
   tcust.rows[2]["custname"] = "http://xml.sz.luohuedu.net/";


   tcust.rows[0]["custcity"] = "北京";
   tcust.rows[1]["custcity"] = "上海";
   tcust.rows[2]["custcity"] = "河南";
  }

  private void addcustomdatatablestyle()
  {
   datagridtablestyle ts1 = new datagridtablestyle();
   ts1.mappingname = "customers";
   // 设置属性
   ts1.alternatingbackcolor = color.lightgray;

   // 添加textbox列样式,以便我们捕捉鼠标事件
   datagridtextboxcolumn textcol = new datagridtextboxcolumn();
   textcol.mappingname = "custid";
   textcol.headertext = "序号";
   textcol.width = 100;

   //添加事件处理器
   textcol.textbox.mousedown += new mouseeventhandler(textboxmousedownhandler);
   textcol.textbox.doubleclick += new eventhandler(textboxdoubleclickhandler);
   ts1.gridcolumnstyles.add(textcol);

本文关键:DataGrid相关知识总结(收集自各位老大处)
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top