DataGrid的用法
来源:互联网 发布:http代理客户端软件 编辑:程序博客网 时间:2024/05/28 11:50
一、自定义列名
private void BindDataGridToArrayList()
{
dataGrid1.DataSource = ScriptPoints;
// create a custom table style
DataGridTableStyle ts = new DataGridTableStyle();
ts.MappingName = "ArrayList";
ts.RowHeaderWidth = 35;
ts.AlternatingBackColor = Color.LightSteelBlue;
DataGridTextBoxColumn cs1 = new DataGridTextBoxColumn();
cs1.MappingName = "DelayTime";
cs1.HeaderText = "延时时间";
cs1.Width = 35;
ts.GridColumnStyles.Add(cs1);
DataGridTextBoxColumn cs2 = new DataGridTextBoxColumn();
cs2.MappingName = "Latitude";
cs2.HeaderText = "纬度";
cs2.Width = 60;
ts.GridColumnStyles.Add(cs2);
DataGridTextBoxColumn cs3 = new DataGridTextBoxColumn();
cs3.MappingName = "Longitude";
cs3.HeaderText = "经度";
cs3.Width = 60;
ts.GridColumnStyles.Add(cs3);
DataGridTextBoxColumn cs4 = new DataGridTextBoxColumn();
cs4.MappingName = "Altitude";
cs4.HeaderText = "海拔高度";
cs4.Width = 60;
ts.GridColumnStyles.Add(cs4);
DataGridTextBoxColumn cs5 = new DataGridTextBoxColumn();
cs5.MappingName = "HT";
cs5.HeaderText = "是否使用视角/倾角";
cs5.Width = 22;
ts.GridColumnStyles.Add(cs5);
DataGridTextBoxColumn cs6 = new DataGridTextBoxColumn();
cs6.MappingName = "Horizontal";
cs6.HeaderText = "水平位置";
cs6.Width = 48;
ts.GridColumnStyles.Add(cs6);
DataGridTextBoxColumn cs7 = new DataGridTextBoxColumn();
cs7.MappingName = "Vertical";
cs7.HeaderText = "垂直位置";
cs7.Width = 48;
ts.GridColumnStyles.Add(cs7);
DataGridTextBoxColumn cs8 = new DataGridTextBoxColumn();
cs8.MappingName = "M";
cs8.HeaderText = "是否显示消息";
cs8.Width = 22;
ts.GridColumnStyles.Add(cs8);
DataGridTextBoxColumn cs9 = new DataGridTextBoxColumn();
cs9.MappingName = "Message";
cs9.HeaderText = "信息";
cs9.Width = 100;
ts.GridColumnStyles.Add(cs9);
DataGridTextBoxColumn cs10 = new DataGridTextBoxColumn();
cs10.MappingName = "HorizontalMessage";
cs10.HeaderText = "水平信息";
cs10.Width = 22;
ts.GridColumnStyles.Add(cs10);
DataGridTextBoxColumn cs11 = new DataGridTextBoxColumn();
cs11.MappingName = "VerticalMessage";
cs11.HeaderText = "垂直信息";
cs11.Width = 22;
ts.GridColumnStyles.Add(cs11);
DataGridTextBoxColumn cs12 = new DataGridTextBoxColumn();
cs12.MappingName = "DisplayMessage";
cs12.HeaderText = "播放信息";
cs12.Width = 22;
ts.GridColumnStyles.Add(cs12);
DataGridTextBoxColumn cs13 = new DataGridTextBoxColumn();
cs13.MappingName = "VerticalExaggeration";
cs13.HeaderText = "垂直渲染";
cs13.Width = 35;
ts.GridColumnStyles.Add(cs13);
DataGridTextBoxColumn cs14 = new DataGridTextBoxColumn();
cs14.MappingName = "LatLonGrid";
cs14.HeaderText = "经纬度网格";
cs14.Width = 22;
ts.GridColumnStyles.Add(cs14);
// add the custom table style
dataGrid1.TableStyles.Clear();
dataGrid1.TableStyles.Add(ts);
}
二、显示行号
private void dataGrid1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)//事件提供数据
{
int row = 0;
int yDelta = dataGrid1.GetCellBounds(0, 0).Height + 1;
int y = dataGrid1.GetCellBounds(0, 0).Top + 2;
CurrencyManager cm = (CurrencyManager)this.BindingContext[dataGrid1.DataSource, dataGrid1.DataMember];
while (y < dataGrid1.Height - yDelta && row < cm.Count)//显示行索引
{
//get & draw the header text...
string text = string.Format("{0}", row);
e.Graphics.DrawString(text, dataGrid1.Font, new SolidBrush(Color.Black), 12, y);
y += yDelta;
row++;
}
}
三、列隐藏
DataGrid1.TableStyles("Products").GridColumnStyles("QuantityPerUnit").Width = 0
or
ds.Tables("Products").Columns("QuantityPerUnit").ColumnMapping = MappingType.Hidden
DataGrid1.SetDataBinding(ds, "Products")
四、单击一个字段选中一行
private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Point pt = new Point(e.X,e.Y);
DataGrid.HitTestInfo hit = dataGrid1.HitTest(pt);
if(hit.Type == DataGrid.HitTestType.Cell)
{
dataGrid1.Select(hit.Row);
}
五、datagrid中下拉列表框
订阅dataGrid的单元格得到当标的事件
2.建立新的控件并绑定
private void dgdFunctionArea_GotFocus(object o, EventArgs e)
{
//Create the combo control to be added and set its properties
comboControl = new ComboBox();
comboControl.Cursor = System.Windows.Forms.Cursors.Arrow;
comboControl.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown;
comboControl.Dock = DockStyle.Fill;
comboControl.Items.AddRange(new string[5]{"","Information Technology","Computer Science","Bio Technology","Electrical Engg"});
//Create the date time picker control to be added and set its properties
DateTimePicker dtp = new DateTimePicker();
dtp.Dock = DockStyle.Fill;
dtp.Cursor = Cursors.Arrow;
//Create the check box control to be added and set its properties
CheckBox chk = new CheckBox();
chk.Dock = DockStyle.Fill;
chk.Cursor = Cursors.Arrow;
//Create the radio button control to be added and set its properties
RadioButton rb = new RadioButton();
rb.Dock = DockStyle.Fill;
rb.Cursor = Cursors.Arrow;
//Add the controls to the respective columns in the data grid
for(int i = 0 ;i < dataTable.Rows.Count ; i++)
{
//if the data in the first column is date time, add a date time control to the grid
if(dataGrid1[i,0].ToString().Equals("DateTime") && hitTestGrid != null && hitTestGrid.Row == i)
{
datagridtextBox.TextBox.Controls.Add(dtp);
comboControl.SendToBack();
chk.SendToBack();
rb.SendToBack();
dtp.BringToFront();
}
//if the data in the first column is combo box, add a combo box control to the grid
else if(dataGrid1[i,0].ToString().Equals("ComboBox") && hitTestGrid != null && hitTestGrid.Row == i)
{
datagridtextBox.TextBox.Controls.Add(comboControl);
chk.SendToBack();
dtp.SendToBack();
rb.SendToBack();
comboControl.BringToFront();
}
//if the data in the first column is check box, add a check box control to the grid
else if(dataGrid1[i,0].ToString().Equals("CheckBox") && hitTestGrid != null && hitTestGrid.Row == i)
{
datagridtextBox.TextBox.Controls.Add(chk);
comboControl.SendToBack();
dtp.SendToBack();
rb.SendToBack();
chk.BringToFront();
}
//if the data in the first column is radio button, add a radio button control to the grid
if(dataGrid1[i,0].ToString().Equals("Radio Button") && hitTestGrid != null && hitTestGrid.Row == i)
{
datagridtextBox.TextBox.Controls.Add(rb);
comboControl.SendToBack();
chk.SendToBack();
dtp.SendToBack();
rb.BringToFront();
}
datagridtextBox.TextBox.BackColor = Color.White;
}
}
六、日期选择器
private void Student_Styles()
{
//#1,这是一个绑定日期获取器的例子,共三步,请看#1,#2,#3
style.GridColumnStyles.AddRange(new DataGridColumnStyle[]{id,room_id,real_name,sex,card,tel,degree,post,address,birth});
dgMain.TableStyles.Add(style);
//订阅单元格获得焦点时发生的事件
dgBox=(DataGridTextBoxColumn)dgMain.TableStyles[0].GridColumnStyles[9];//哪一列出现
dgBox.TextBox.GotFocus+= new EventHandler(this.dgBox_GotFocus);
//
}
//#2
private void dgBox_GotFocus(object o, EventArgs e)
{
DateTimePicker dtp = new DateTimePicker();
dtp.Dock = DockStyle.Fill;
dtp.Cursor = Cursors.Arrow;
dtp.ValueChanged+=new EventHandler(this.dtg_ValueChanged);//日期获取器变化
dgBox.TextBox.Controls.Add(dtp);
dtp.BringToFront();
}
//#3
private void dtg_ValueChanged(object sender, System.EventArgs e)
{
int lineNum=dgMain.CurrentCell.RowNumber;//或dgMain.CurrentRowIndex
dgMain[lineNum,9]=((DateTimePicker)(sender)).Value.ToShortDateString();
}
- DataGrid的用法问题
- DataGrid的用法
- DataGrid的用法
- DataGrid的高级用法
- DataGrid的高级用法
- DataGrid 的用法
- datagrid的用法
- DataGrid按钮列的用法
- DataGrid的ItemDataBound用法实例
- WinForm DataGrid 的DataGridTableStyle用法
- WinForm DataGrid 的DataGridTableStyle用法
- [WPF] DataGrid的用法小结
- DataGrid中的ItemTemplate的用法
- Silverlight中DataGrid的用法
- Flex4 <s:DataGrid> 的用法
- jquery datagrid 的一些用法
- WPF的MVVM DataGrid用法
- DataGrid的用法(一)基本用法
- 重装操作系统前后注意事项!
- Windows Vista九则加速技巧
- 文件拖动Drag &amp; Drop 全解析
- PHPLivex
- 软件开发管理与质量控制
- DataGrid的用法
- asp.net2.0中泛型的使用
- Tomcat:IOException while loading persisted sessions: java.io.EOFException异常处理
- java中的时间操作
- 十条软件缺陷现状
- 程序员网站资源宝库
- cmd shell 重定向到网络
- 对于文件拷贝时的两个实用函数
- 解决Java读取properties文件的中文问题的新办法(不使用native2ascii.exe及其他工具)