C# winform DatagridView 的简单操作
2016-02-28 15:21
417 查看
数据显示操作:
dgBill.Columns[0].DataPropertyName = "key1";
dgBill.Columns[1].DataPropertyName = "key2";
dgBill.Columns[2].DataPropertyName = "key3";
DataTable dt = new DataTable();
dt.Columns.Add("key1", Type.GetType("System.String"));
dt.Columns.Add("key2", Type.GetType("System.String"));
dt.Columns.Add("key3", Type.GetType("System.String"));
DataRow dr = null;
dr = dt.NewRow();
dr["key1"] = "key1";
dr["key1"] = "key2";
dr["key1"] = "key3";
dt.Rows.Add(dr);
this.dgBill.DataSource = dt;
选择一行数据:
//右键菜单事项
private void dg1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (e.RowIndex >= 0)
{
//若行已是选中状态就不再进行设置
dg1.ClearSelection();
dg1.Rows[e.RowIndex].Selected = true;
//设置当前行
dg1.CurrentCell = dg1.Rows[e.RowIndex].Cells[0];
//弹出操作菜单
contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
}
}
}
//选择菜单的时候,进行获取右键一行的数据
DataGridViewRow rowData = dgBill.CurrentRow;
if (rowData != null)
{
if (rowData.Cells["ID"].Value != null)
{
}
else
{
MessageBox.Show("数据转换异常", "提示", MessageBoxButtons.OK);
}
}
dgBill.Columns[0].DataPropertyName = "key1";
dgBill.Columns[1].DataPropertyName = "key2";
dgBill.Columns[2].DataPropertyName = "key3";
DataTable dt = new DataTable();
dt.Columns.Add("key1", Type.GetType("System.String"));
dt.Columns.Add("key2", Type.GetType("System.String"));
dt.Columns.Add("key3", Type.GetType("System.String"));
DataRow dr = null;
dr = dt.NewRow();
dr["key1"] = "key1";
dr["key1"] = "key2";
dr["key1"] = "key3";
dt.Rows.Add(dr);
this.dgBill.DataSource = dt;
选择一行数据:
//右键菜单事项
private void dg1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (e.RowIndex >= 0)
{
//若行已是选中状态就不再进行设置
dg1.ClearSelection();
dg1.Rows[e.RowIndex].Selected = true;
//设置当前行
dg1.CurrentCell = dg1.Rows[e.RowIndex].Cells[0];
//弹出操作菜单
contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
}
}
}
//选择菜单的时候,进行获取右键一行的数据
DataGridViewRow rowData = dgBill.CurrentRow;
if (rowData != null)
{
if (rowData.Cells["ID"].Value != null)
{
}
else
{
MessageBox.Show("数据转换异常", "提示", MessageBoxButtons.OK);
}
}
相关文章推荐
- C#找质数(素数)
- c#如何采集需要登录的页面
- C# 反射详解
- C# 反射详解
- C# 命名规范
- C#WinForm打开文件夹并选中文件
- C#demo程序 调用自己编写的DLL
- C#委托
- C# 动态加载程序集dll (实现接口)
- C#生成exe、dll版本号自动增加
- C# AD(Active Directory)域信息同步,组织单位、用户等信息查询
- C# 指南之装箱与拆箱
- C#综合揭秘——细说多线程(下)
- 2.C#中泛型在方法Method上的实现
- C#综合揭秘——细说多线程(上)
- 1.什么是泛型和C#中泛型在Class上的实现
- 七层登录——C#
- 订阅者方法超时的处理
- [hdu5632][BC#73 1002]Rikka with Array
- C# 枚举、字符串、值的相互转换