当前位置:编程学习 > C#/ASP.NET >>

vs2003下, DataGrid如何添加label的问题!

请问:vs2003下, DataGrid如何添加一个label,并且跟数据表中的某个字段绑定,并可以响应单击事件 --------------------编程问答-------------------- 可以去查下MSDN,,但是03的好久以前用过。现在都是05了,LZ怎么还在用?? --------------------编程问答-------------------- 没办法呀,唉,其实挺不喜欢用.net做winform的 --------------------编程问答-------------------- 1.1的时候DataGrid用DataGridTableStyle来控制模板和样式
添加一列可以用DataGridTextboxColumn类型,加上只读属性就是你要的label,它的MappingName属性对应到数据列上
要单元格的事件用currentcellchanged事件

private void Form1_Load(object sender, System.EventArgs e)
{
RenderGrid();
this.dataGrid1.DataSource = GetData();


}

private void RenderGrid()
{
DataGridTableStyle tableStyle = new DataGridTableStyle();

DataGridTextBoxColumn col = new DataGridTextBoxColumn();
col.HeaderText = "名称";
col.MappingName = "Name";
col.ReadOnly = true;

tableStyle.GridColumnStyles.Add(col);
this.dataGrid1.TableStyles.Add(tableStyle);
}

private object GetData()
{
DataTable table = new DataTable();
DataColumn col = new DataColumn("Name", typeof(string));
table.Columns.Add(col);

DataRow row = table.NewRow();
row[0] = "hello";
table.Rows.Add(row);

row = table.NewRow();
row[0] = "world";
table.Rows.Add(row);

return table;

}

private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
{
MessageBox.Show(this.dataGrid1[dataGrid1.CurrentCell].ToString());
}
--------------------编程问答-------------------- 其实要的是那种超链接列的显示效果 --------------------编程问答-------------------- 不理解。逛了 --------------------编程问答-------------------- 我建议你用 DATALIST
去绑定


不要用DATAGRID
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,