会员
周边
众包
新闻
博问
闪存
赞助商
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
bearhb
博客园
首页
新随笔
联系
订阅
管理
看上去没有输入焦点的只读DataGrid (WinForm)
假设已经在WinForm窗体上放置了一个名称为dataGridMsg的DataGrid,并且为窗体增加一个成员
private
DataSet DataSetMsg;
在form的Load中添加以下代码
DataSetMsg
=
new
DataSet(
"
DataSetMsg
"
);
DataTable dt
=
DataSetMsg.Tables.Add(
"
DataTableMsg
"
);
dt.Columns.Add(
"
Code
"
,
typeof
(
string
));
dt.Columns.Add(
"
Msg
"
,
typeof
(
string
));
dt.Columns.Add(
"
V
"
,
typeof
(
string
));
//
添加用于隐藏输入焦点的列
//
添加10行
for
(
int
B
=
0
;B
<
10
,B
++
)
{
DataRow dr
=
new
dt.NewRow();
dr[
"
Code
"
]
=
B.ToString().PadLeft(
4
,
'
0
'
);
dr[
"
Msg
"
]
=
"
Msg
"
+
B.ToString();
dr[
"
V
"
]
=
""
;
}
dataGridMsg.ReadOnly
=
true
;
//
指定DataGridMsg列的外观
DataView dataviewMsg
=
new
DataView(DataSetMsg.Tables[
"
DataTableMsg
"
]);
dataviewMsg.AllowDelete
=
false
;
dataviewMsg.AllowNew
=
false
;
dataGridMsg.SetDataBinding(dataviewMsg,
""
);
DataGridTableStyle ts1
=
new
DataGridTableStyle(
false
);
ts1.MappingName
=
"
DataTableMsg
"
;
ts1.AllowSorting
=
true
;
ts1.GridColumnStyles.Clear();
//
第0列--代码
DataGridTextBoxColumn CCode
=
new
DataGridTextBoxColumn();
CCode.MappingName
=
"
Code
"
;
CCode.HeaderText
=
"
代码
"
;
CCode.Width
=
100
;
CCode.Alignment
=
HorizontalAlignment.Center;
CCode.ReadOnly
=
true
;
ts1.GridColumnStyles.Add(CCode);
//
第1列-- 信息
DataGridTextBoxColumn Cmsg
=
new
DataGridTextBoxColumn();
Cmsg.MappingName
=
"
Msg
"
;
Cmsg.HeaderText
=
"
信息
"
;
Cmsg.ReadOnly
=
true
;
Cmsg.Alignment
=
HorizontalAlignment.Left;
Cmsg.Width
=
600
;
ts1.GridColumnStyles.Add(Cmsg);
//
第2列--用于隐藏输入焦点的列
DataGridTextBoxColumn CV
=
new
DataGridTextBoxColumn();
CV.MappingName
=
"
V
"
;
CV.ReadOnly
=
true
;
CV.Width
=
0
;
//
隐藏的列
ts1.GridColumnStyles.Add(CV);
dataGridMsg.TableStyles.Clear();
dataGridMsg.TableStyles.Add(ts1);
在 dataGridMsg的CurrentCellChanged 事件中添加以下代码
//
将当前列始终设置为定义的隐藏列2
dataGridMsg.CurrentCell
=
new
DataGridCell(dataGridMsg.CurrentCell.RowNumber,
2
);
posted @
2005-08-04 17:28
阿土仔
阅读(
591
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
公告