国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

在DataGrid中簡單使用下拉列表框

2024-07-21 02:23:38
字體:
來源:轉載
供稿:網友
在datagrid中簡單使用下拉列表框

作者:tushar ameta

翻譯:秋楓

在datagrid中使用下拉列表問題。這篇文章講了如何在 system.windows.forms.datagrid中切入使用combobox控件。不過原文不全,無法調試,在這里為了說清楚點,對原文作了一些修改,整篇文章主要包括三方面的內容。

1. 在datagrid中加入combobox列;

2. 把在datagrid中的修改保存到對應的網格;

3. 設置datagrid中網格的焦點。



下面是整個源代碼,一些功能可以看注釋。

using system;

using system.drawing;

using system.collections;

using system.componentmodel;

using system.windows.forms;

using system.data;



namespace datagridtest

{

public class form1 : system.windows.forms.form

{

private system.windows.forms.datagrid dgdfunctionarea;

private datatable dtblfunctionalarea;

private system.windows.forms.button buttonfocus;

private system.componentmodel.container components = null;



public form1()

{

initializecomponent();

populategrid();

}



protected override void dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.dispose();

}

}

base.dispose( disposing );

}



#region windows 窗體設計器生成的代碼

private void initializecomponent()

{

this.dgdfunctionarea = new system.windows.forms.datagrid();

this.buttonfocus = new system.windows.forms.button();

((system.componentmodel.isupportinitialize)(this.dgdfunctionarea)).begininit();

this.suspendlayout();

//

// dgdfunctionarea

//

this.dgdfunctionarea.datamember = "";

this.dgdfunctionarea.headerforecolor = system.drawing.systemcolors.controltext;

this.dgdfunctionarea.location = new system.drawing.point(4, 8);

this.dgdfunctionarea.name = "dgdfunctionarea";

this.dgdfunctionarea.size = new system.drawing.size(316, 168);

this.dgdfunctionarea.tabindex = 0;

//

// buttonfocus

//

this.buttonfocus.location = new system.drawing.point(232, 188);

this.buttonfocus.name = "buttonfocus";

this.buttonfocus.size = new system.drawing.size(84, 23);

this.buttonfocus.tabindex = 1;

this.buttonfocus.text = "獲取焦點";

this.buttonfocus.click += new system.eventhandler(this.buttonfocus_click);

//

// form1

//

this.autoscalebasesize = new system.drawing.size(6, 14);

this.clientsize = new system.drawing.size(332, 217);

this.controls.add(this.buttonfocus);

this.controls.add(this.dgdfunctionarea);

this.name = "form1";

this.text = "form1";

((system.componentmodel.isupportinitialize)(this.dgdfunctionarea)).endinit();

this.resumelayout(false);



}

#endregion



/// <summary>

/// 應用程序的主入口點。

/// </summary>

[stathread]

static void main()

{

application.run(new form1());

}

//初始化datagrid

private void populategrid()

{

//創建一個datatable對象,包括四列,前三列為string,最后一列為boolean。

dtblfunctionalarea = new datatable ("functionarea");

string[] arrstrfunctionalarea = new string [3]{"functional area","min","max"};

datacolumn dtcol = null;

//創建string列

for(int i=0; i< 3;i++)

{

dtcol = new datacolumn(arrstrfunctionalarea[i]);

dtcol.datatype = type.gettype("system.string");

dtcol.defaultvalue = "";

dtblfunctionalarea.columns.add(dtcol);

}



//創建boolean列,用checkedbox來顯示。

datacolumn dtccheck = new datacolumn("ismandatory");

dtccheck.datatype = system.type.gettype("system.boolean");

dtccheck.defaultvalue = false;

dtblfunctionalarea.columns.add(dtccheck);



//把表綁定到datagrid

dgdfunctionarea.datasource = dtblfunctionalarea;



//為datagrid加載datagridtablestyle樣式

if(!dgdfunctionarea.tablestyles.contains("functionarea"))

{

datagridtablestyle dgdtblstyle = new datagridtablestyle();

dgdtblstyle.mappingname = dtblfunctionalarea.tablename;

dgdfunctionarea.tablestyles.add(dgdtblstyle);

dgdtblstyle.rowheadersvisible = false;

dgdtblstyle.headerbackcolor = color.lightsteelblue;

dgdtblstyle.allowsorting = false;

dgdtblstyle.headerbackcolor = color.fromargb(8,36,107);

dgdtblstyle.rowheadersvisible = false;

dgdtblstyle.headerforecolor = color.white;

dgdtblstyle.headerfont = new system.drawing.font("microsoft sans serif", 9f,

system.drawing.fontstyle.bold,

system.drawing.graphicsunit.point, ((system.byte)(0)));

dgdtblstyle.gridlinecolor = color.darkgray;

dgdtblstyle.preferredrowheight = 22;

dgdfunctionarea.backgroundcolor = color.white;



//設置列的寬度

gridcolumnstylescollection colstyle = dgdfunctionarea.tablestyles[0].gridcolumnstyles;

colstyle[0].width = 100;

colstyle[1].width = 50;

colstyle[2].width = 50;

colstyle[3].width = 80;

}



datagridtextboxcolumn dgtb = (datagridtextboxcolumn)dgdfunctionarea.tablestyles[0].gridcolumnstyles[0];

combobox cmbfunctionarea = new combobox();

cmbfunctionarea.items.addrange(new object[]{"選項一","選項二","選項三"});

cmbfunctionarea.cursor = cursors.arrow;

cmbfunctionarea.dropdownstyle= comboboxstyle.dropdownlist;

cmbfunctionarea.dock = dockstyle.fill;

//在選定項發生更改并且提交了該更改后發生

cmbfunctionarea.selectionchangecommitted += new eventhandler(cmbfunctionarea_selectionchangecommitted);

//把combobox添加到datagridtablestyle的第一列

dgtb.textbox.controls.add(cmbfunctionarea);



}

//設置焦點模擬

private void getfocus(int row,int col)

{

//先把焦點移動到datagrid

this.dgdfunctionarea.focus();

//把焦點移動到datagridcell

datagridcell dgc = new datagridcell(row,col);

this.dgdfunctionarea.currentcell = dgc;

datagridtextboxcolumn dgtb = (datagridtextboxcolumn)dgdfunctionarea.tablestyles[0].gridcolumnstyles[col];

//設置焦點

dgtb.textbox.focus();



}

//把combobox上修改的數據提交到當前的網格

private void cmbfunctionarea_selectionchangecommitted(object sender, eventargs e)

{

this.dgdfunctionarea[this.dgdfunctionarea.currentcell] = ((combobox)sender).selecteditem.tostring();

}

//設置新的焦點

private void buttonfocus_click(object sender, system.eventargs e)

{

//焦點模擬,這里設置第三行第一列

getfocus(2,0);

}

}

}


下面是測試界面:





總結,這里是通過datagridtextboxcolumn.textbox.controls.add方法實現在列中添加combobox控件;對于數據的保存是使用combobox.selectionchangecommitted事件來完成;設置焦點是通過datagridtextboxcolumn.textbox.focus方法來實現。另外通過這個方法也可以添加datetimepicker等類似的控件。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 古浪县| 永胜县| 平湖市| 无棣县| 彭泽县| 庐江县| 隆德县| 青海省| 长宁区| 磴口县| 绍兴县| 烟台市| 舒城县| 宝鸡市| 双牌县| 舒兰市| 黄龙县| 秭归县| 应用必备| 恩平市| 崇礼县| 防城港市| 阳高县| 永修县| 澄江县| 南皮县| 孟村| 原平市| 佛山市| 定远县| 乐清市| 应城市| 卢湾区| 巴林左旗| 天津市| 霍邱县| 寻乌县| 梧州市| 乌拉特前旗| 郓城县| 班玛县|