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

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

本文简介:选择自 jigee 的 blog

分页:
public void dg_pageindexchanged(object source, system.web.ui.webcontrols.datagridpagechangedeventargs e)
{
mydatagrid.currentpageindex = e.newpageindex;
binddata();
}
编辑:
public void edit(object sender,datagridcommandeventargs e)
{
type_dg.edititemindex=e.item.itemindex;
binddata();
}
public void update(object sender,datagridcommandeventargs e)

textbox id=(textbox)e.item.cells[0].controls[0];
textbox name=(textbox)e.item.cells[1].controls[0];
textbox num=(textbox)e.item.cells[2].controls[0];
string update_oledb="update blog_type set b_type_name='"+name.text+"',b_type_num='"+num.text+"' where id='"+id.text+"'";
oledbconnection myconn=new oledbconnection(application["strconn"].tostring());
myconn.open();
oledbcommand update_com=new oledbcommand(update_oledb,myconn);
update_com.executenonquery();

type_dg.edititemindex=-1;
myconn.close();
binddata();
}

public void cancel(object sender,datagridcommandeventargs e)
{
type_dg.edititemindex=-1;
binddata();
}


删除:
public void delete(object sender,datagridcommandeventargs e)
{
string no=type_dg.items[e.item.itemindex].cells[0].text;
oledbconnection myconn=new oledbconnection(application["strconn"].tostring());
myconn.open();
string deleteoledb="delete from blog_type where id="+no;
oledbcommand deletecom=new oledbcommand(deleteoledb,myconn);
deletecom.executenonquery();
myconn.close();
binddata();
}

在winform的datagrid中如何更改列的标题和设置列内容。


表:
id  name strdate   enddate     
1   a     2004/2/1  2005/2/1 

要求显示出来
名称     开始日期        结束日期     逾期(是/否)
a        2004/2/1       2005/2/1      逾期365天
谢谢!


private void form1_load(object sender, system.eventargs e)
{
  sqlconnection cs = new sqlconnection("server=mengxianhui;database=sqlpub2;user id=sa;password=");
  sqldataadapter mycommand = new sqldataadapter("select lastmodified,objectid from baseobject", cs);
  dataset mydataset = new dataset();
  mycommand.fill(mydataset, "baseobject");
  this.datagrid1.datasource = mydataset.tables[0];
  //设置datagrid的各列
  datagridtextboxcolumn c1=new datagridtextboxcolumn();
  datagridtextboxcolumn c2=new datagridtextboxcolumn();

  c1.mappingname = "lastmodified";
  c2.mappingname = "objectid";

  c1.headertext="时间【你好,夏威夷】";
  c2.headertext="标号";
  c1.format="yyyy年mm月dd日";
  c1.width = 200;

  datagridtablestyle dts=new datagridtablestyle();
  dts.gridcolumnstyles.add(c1);
  dts.gridcolumnstyles.add(c2);
  dts.mappingname="baseobject";
  this.datagrid1.tablestyles.add(dts);
}

主  题:  datagrid+checkboxlist应用问题 

你可以添加一个模板列,然后对模板列进行编辑,加入一个checkbox和一个checkboxlist,然后命名这两个控件,在code加入checkbox1_checkedchanged事件,把checkbox的auto postback设置为true,再在
checkbox1_checkedchanged事件写你所要实现的功能。


下面是一个简单例子,删除datagrid当前行数据:(前面数据已经绑定到datagrid1了)

string strsql = "delete from table1 where id=@id";
string text = datagrid1[datagrid1.currentcell.rownumber, 0].tostring();
testdataset1.table1.rows[datagrid1.currentcell.rownumber].delete();
testdataset1.acceptchanges();
sqldataadapter2.deletecommand.parameters["@id"].value = text;
sqldataadapter2.deletecommand.connection.open();
sqldataadapter2.deletecommand.executenonquery();
sqldataadapter2.deletecommand.connection.close();


主  题:  请教在datagridview中怎样生成自适应的列宽?

自适应 列宽:

'控制dategrid列宽度函数
    public sub sizecolumnstocontent(byval datagrid as datagrid, byval nrowstoscan as integer)
        dim graphics as graphics = datagrid.creategraphics()
        dim tablestyle as datagridtablestyle = new datagridtablestyle

        try

            dim datatable as datatable = ctype(datagrid.datasource, datatable)

            if -1 = nrowstoscan then

                nrowstoscan = datatable.rows.count

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

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

go top