(5)C#工具箱-数据

时间:2022-12-08 14:59:57

1.DataSet

 

2.DataGridView

dataGridView是一个显示网络数据的控件

(1)绑定dataSet

DataSet ds = new DataSet();
//执行数据库查询,返回一个dataset
ds = DB.Query();
//服务端,给dataset的table[0]表起名叫tab。绑定时一定要带上定义的表名
//光标默认定位在第一行第一列
dataGridView1.DataSource = ds.Tables["tab"];

 

(5)C#工具箱-数据

(2)修改字体和颜色

//更改字体
dataGridView1.DefaultCellStyle.Font = new Font("宋体", 10);
//更改某单元格的字体颜色
dataGridView1.Rows[1].Cells[1].Style.ForeColor = Color.Red;
//更改某单元格的背景颜色
dataGridView1.Rows[0].Cells[1].Style.BackColor = Color.Pink;
//选中单元格时的背景色
dataGridView1.Rows[1].Cells[0].Style.SelectionBackColor = Color.Red;

(3)获得值和索引

 选中单个表格或单个行号、列号选中时。当选中多数据时,只能获得第一个

//获得当前单元格的值
string a = dataGridView1.CurrentCell.Value.ToString();
//获得当前单元格所在列索引
int b = dataGridView1.CurrentCell.ColumnIndex;
//获得当前单元格所在行索引
int c = dataGridView1.CurrentCell.RowIndex;
// 设定 (列, 行) 为当前单元格
dataGridView1.CurrentCell = dataGridView1[3, 1];

   选中多格或多行、多列

 (4)设置单元格可读、可写性

// 设置 DataGridView1 为只读,用户无法在单元格输入
dataGridView1.ReadOnly = true;

 

//用户无法选择多行或者多列
dataGridView1.MultiSelect = false;

 

//滚动条显示类型 左右、垂直、全部、全不
dataGridView1.ScrollBars = ScrollBars.Both;

 

  (5)隐藏

 

(6)增加、删除、修改

 

 

(7)事件

(5)C#工具箱-数据

上百个事件,有空再看

 

3.BindingSource

 

4.BindingNavigator

把控件拖到dataGridView里,制作翻页和新增、删除

 

(5)C#工具箱-数据

 

5.Chart 图表控件