初始DataGridView

  • DataGridView控件提供了一种强大而灵活的以表格形式显示数据的方式。用户可以使用DataGridView控件来显示少量数据的只读视图,也可以对齐进行缩放以显示特大数据集的可标记视图。
  • 扩展DataView控件由很多方式,例如可以采用编程方式指定自己的排序算法来创建自己的单元格类型,通过选择一些属性,可以轻松地自定义DataGridView控件的外观:可以将许多类型的数据存储用作数据源:也可以在没有绑定数据源的情况下操作DataGridView控件。
  • 配置数据源









运行前只显示字段,运行后显示具体数据

常用属性


2、绑定数据方法

  • Data Grid view控件用于显示来自多种外部数据源中的数据,用户可在此空间中添加行和列,并可以填充数据。
  • 入股让DataGrideView显示数据库中的数据,只需要将此控件绑定到代用数据库的数据院上,则可以自动基于数据源的架构生成列。
  • 绑定模式两种方法:
    • 绑定模式:将已存在的数据库中数据绑定到空间上
    • 非绑定模式:通过代码手动填充数据

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;namespace _2_DataGridView绑定数据源
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){dgvbindMode.DataSource = BindModSource().Tables[0];dgvNonbindMode.DataSource = NonBindSource();}private DataSet BindModSource()   //绑定模式,有线程数据库{string constr = "Server=.;user=sa;pwd=darly;database=csharpzx";SqlConnection mycon = new SqlConnection(constr);DataSet myds = new DataSet();try{mycon.Open();string sql = "select name,gender from mytable";SqlDataAdapter myda = new SqlDataAdapter(sql, mycon);myda.Fill(myds, "mytable");}catch (Exception ex){MessageBox.Show(ex.Message.ToString(), "错误提示");}finally{mycon.Close();}return myds;}private DataTable NonBindSource()   //非绑定模式,没有数据库{DataTable mydt = new DataTable();mydt.Columns.Add("name", Type.GetType("System.String"));mydt.Columns.Add("gender", Type.GetType("System.String"));string[,] mystr = { { "张三", "女" }, { "李四", "女" }, { "王五", "女" }, { "赵六", "女" }, { "张祺", "女" }};for (int i = 0; i < mystr.Length / 2; i++){DataRow myRow = mydt.NewRow();myRow[0] = mystr[i, 0];myRow[1] = mystr[i, 1];mydt.Rows.Add(myRow);}return mydt;}}
}

3、获取当前单元格

  • 获取DataGridView控件中的当前单元格,是通过DataGridView的Rows属性和Colunm属性的索引来取得的,他们的索引都是从0开始的。

实例,当点击单元格时获取到点击的是第几行第几列,并获取单元格内的值


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace _3_获取单元格
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){// TODO: 这行代码将数据加载到表“csharpzxDataSet1.mytable”中。您可以根据需要移动或删除它。this.mytableTableAdapter.Fill(this.csharpzxDataSet1.mytable);}//方法的定义中第一个参数 sender就是指的当前操作的控件,第二个参数就是用于返回当前操作控件内容值private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e){//获取行列索引//1种方式//int row = e.RowIndex + 1;   //获取的行数;//int col = e.ColumnIndex + 1;   //获取列数//MessageBox.Show("你点击的是:第" + row.ToString() + "行,第" + col.ToString() + "列。");//第2中方式//int row2 = dataGridView1.CurrentCell.RowIndex + 1;//int col2 = dataGridView1.CurrentCell.ColumnIndex + 1;//MessageBox.Show("你点击的是:第" + row2.ToString() + "行,第" + col2.ToString() + "列。");//第3中方式//int row3 = dataGridView1.CurrentCellAddress.Y + 1;int col3 = dataGridView1.CurrentCellAddress.X + 1;//MessageBox.Show("你点击的是:第" + row3.ToString() + "行,第" + col3.ToString() + "列。");//第四种方式int row4 = dataGridView1.CurrentRow.Index + 1;   //此方法仅适用于行,不适用于列。//获取单元格内容Value//第一种方式//string cell = dataGridView1.Rows[row4-1].Cells[col3-1].Value.ToString();//第二种方式string cell = dataGridView1.CurrentCell.Value.ToString();MessageBox.Show("你点击的是:第" + row4.ToString() + "行,第" + col3.ToString() + "列。\n内容是:"+cell);}}
}

4、DataGridView隐藏行、列

隐藏与显示
visible: true为显示,false为隐藏
Rows:行
Columns:列

using System;
using System.Windows.Forms;namespace _4_DataGridView隐藏与显示行和列
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){// TODO: 这行代码将数据加载到表“csharpzxDataSet.mytable”中。您可以根据需要移动或删除它。this.mytableTableAdapter.Fill(this.csharpzxDataSet.mytable);}private void button1_Click(object sender, EventArgs e){string mystr = button1.Text;if (mystr.Contains("隐藏")){//获取单元格所在的列//int col = dataGridView1.CurrentCell.ColumnIndex;dataGridView1.Columns[4].Visible = false;button1.Text = "显示部门列";}else{dataGridView1.Columns[4].Visible = true;button1.Text = "隐藏部门列";}}private void button2_Click(object sender, EventArgs e){string mystr = button2.Text;if (mystr.Contains("隐藏")){dataGridView1.Rows[1].Visible = false;button2.Text = "显示张三行";}else{dataGridView1.Rows[1].Visible = true;button2.Text = "隐藏张三行";}}}
}

5、DataGridView右键删除行

右键:ContextMenuStrip
删除行DataGridView1.Rows.RemoveAt(行索引值);

using System;
using System.Windows.Forms;namespace DataGrideView右键删除行
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){// TODO: 这行代码将数据加载到表“csharpzxDataSet.mytable”中。您可以根据需要移动或删除它。this.mytableTableAdapter.Fill(this.csharpzxDataSet.mytable);}private int rowIndex = 0;private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e){if (e.Button == MouseButtons.Right) //判断鼠标操作事件,是左键还是右键{this.dataGridView1.Rows[e.RowIndex].Selected = true;    //选中当前行//如果没有下面命令,每次点击会累加选择,出现多条行记录被选中的情况,下面命令就是右击是定义那个单元格为当前单元格;this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[0];rowIndex = e.RowIndex;  //将当前行的索引值赋值给公共变量,以便删除时判断是否为空行(新行)//this.contextMenuStrip1.Show(this.dataGridView1, e.Location);   //第一个参数是指定那个控件,第二个参数是当前事件发生的地方this.contextMenuStrip1.Show(Cursor.Position);  //鼠标点击出显示}}private void 删除行ToolStripMenuItem_Click(object sender, EventArgs e){if (!this.dataGridView1.Rows[rowIndex].IsNewRow){this.dataGridView1.Rows.RemoveAt(rowIndex);}}}
}

6、DataGridView分类排序与筛选

分类: Sort
筛选:通过声明DataView,更改DataSource,

using System;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;namespace _6__DataGridView升序与筛选
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){// TODO: 这行代码将数据加载到表“csharpzxDataSet.mytable”中。您可以根据需要移动或删除它。this.mytableTableAdapter.Fill(this.csharpzxDataSet.mytable);}private void button1_Click(object sender, EventArgs e){dataGridView1.Sort(dataGridView1.Columns[3], ListSortDirection.Ascending);  //第一个参数是按照哪一个列拍序,第二个参数是升序还是降序:Descending-降序,Ascending升序}private void button2_Click(object sender, EventArgs e){//参数1源数据表,参数2筛选条件,参数3筛选后的排列方式,参数4当前行的状态(dataview的状态,指定为当前行的状态)DataView dv = new DataView(this.csharpzxDataSet.mytable, "department='开发部'", "age Asc", DataViewRowState.CurrentRows);//上面语句可以简单理解,将源数据进行筛选,下面的语句是将筛选出来的数据绑定到DataGridView中。dataGridView1.DataSource = dv;}}
}

7、DataGridView直接修改数据

  • DataGridView单元格进入编辑模式的方法:

    • 双击或F12;
    • EditCellonEnter属性为True,对DataGridView进行配置,当用户移动到该单元个后,该单元格颗粒剂切换到编辑模式。
    • ESC取消编辑。如果EditCellOnEnter属性设置True,则单元格仍将处于编辑模式,但是所有更改都将被放弃。要提交更改,用户只需移到新的单元格,或将焦点切换到其他控件。
  • 为防止单元格被编辑,用户可以设置
  • DataGridViewCell、DataGridViewColumn、DataGrieViewRow或DataGridView的ReadOnly属性(区域与用户要先定更改的内容)
  • 要在DataGridViwe控件中修改数据,可通过CellValueChanged事件来完成
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;namespace _7_DataGridView直接修改数据
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private SqlConnection GeConnection(){string constr = "Server=.;user=sa;pwd=darly;database=csharpzx";SqlConnection mycon = new SqlConnection(constr);return mycon;}private void dgvBind(){SqlConnection mycon = GeConnection();try{mycon.Open();SqlDataAdapter sda =new  SqlDataAdapter("select * from mytable", mycon);DataTable table = new DataTable();sda.Fill(table);this.dataGridView1.AutoGenerateColumns = true;  //设置DataGridView自动创建列this.dataGridView1.DataSource = table;this.dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;}catch(Exception ex){MessageBox.Show(ex.Message);}finally{mycon.Close();}}private void Form1_Load(object sender, EventArgs e){dgvBind();}private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e){SqlConnection mycon = GeConnection();try{mycon.Open();string mystr1=dataGridView1.Columns[e.ColumnIndex].HeaderText+"="+"'"+dataGridView1.CurrentCell.Value.ToString()+"'";  //获取档前单元格的所在列的标头string mystr2=dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();string updatsql = "update mytable set "+mystr1+" where id = "+mystr2;MessageBox.Show(updatsql);SqlCommand mycom = new SqlCommand(updatsql, mycon);mycom.ExecuteNonQuery();dgvBind();}catch (Exception ex){MessageBox.Show(ex.Message);}finally{mycon.Close();}}}
}

8、DataGridView当前行显示不同颜色

如果想让选中的DataGridView的行显示不同颜色,就要通过DataGridView控件RowPrePaint事件中重新设置所选的DefaultCellStyle属性来实现

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace _8_DataGridView当前行显示不同的颜色
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){// TODO: 这行代码将数据加载到表“csharpzxDataSet.mytable”中。您可以根据需要移动或删除它。this.mytableTableAdapter.Fill(this.csharpzxDataSet.mytable);}private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e){if (e.RowIndex >= dataGridView1.Rows.Count - 1) return;  //如果选中的行是一个新行(待添加内容)不进行操作,直接返回Color oldForeColor = new Color();Color oldBackColor = new Color();// object是所有类的基类, var是可以是所有变量类型,不知道变量类型时,可以声明为var格式var row = dataGridView1.Rows[e.RowIndex];    //操作的行if (row == dataGridView1.CurrentRow)   //判断操作行与当前行是不是一行,如果一样代表是首次操作,所以要绘制不同样式{if (row.DefaultCellStyle.ForeColor != Color.White) ;{oldForeColor = row.DefaultCellStyle.ForeColor;  // 存储现在的前景色row.DefaultCellStyle.ForeColor = Color.White;   // 设置前景色为白色}if (row.DefaultCellStyle.BackColor != Color.Blue){oldBackColor = row.DefaultCellStyle.BackColor;row.DefaultCellStyle.BackColor = Color.Blue;}}else{row.DefaultCellStyle.ForeColor = oldForeColor;row.DefaultCellStyle.BackColor = oldBackColor;}}}
}

9、DataGridView绘制行号

绘制DataGridView的行序列号
RowPostPaint事件(当我们完成DataGridView绘制后执行的事件),通过Rectangle对象矩形区域,然后再通过TextRenderer的DrawText方法绘制序列号
Rectangle(x,y,width,height)
DrawText(IDeviceContext dc, text,font,Rectangle bounds, foreColor,TextFormatFlags) 第一个参数(IDeviceContext dc)是在哪个控件上进行绘制,Rectangle bounds矩形边界,TextFormatFlags字体对齐方式

using System;
using System.Drawing;
using System.Windows.Forms;namespace _9_DataGridView添加行序号
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){// TODO: 这行代码将数据加载到表“csharpzxDataSet.mytable”中。您可以根据需要移动或删除它。this.mytableTableAdapter.Fill(this.csharpzxDataSet.mytable);}private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e){//e代表DataGridView,RowBound控件行bianjieRectangle myrec = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dataGridView1.RowHeadersWidth-5, e.RowBounds.Height);   //宽度值减小是为了内容与下一列间隔开TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dataGridView1.RowHeadersDefaultCellStyle.Font, myrec, dataGridView1.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);}}
}

10、各行显示不同颜色

两种方法:
第一种 DataGridView1.Rows[i].DefaultCellStyle.BAckColor
第二种:AlternatingRowsDefaultCellStyle属性,获取或这是应用于DataGridView的奇数行的默认单元格样式。RowsDefaultCellStyle属性获取或设置DataGridView的行单元格的默认样式

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace _10_DataGridView各行显示不同的颜色
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){// TODO: 这行代码将数据加载到表“csharpzxDataSet.mytable”中。您可以根据需要移动或删除它。this.mytableTableAdapter.Fill(this.csharpzxDataSet.mytable);第一种方式//for (int i=0; i<this.dataGridView1.Rows.Count;i++)//{//    if (i % 2 == 0)//        this.dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Pink;//    else//        this.dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Violet;//}//第二种this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.Violet;this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.SaddleBrown;}}
}

11、DataGridView分页显示

思路:将数据表整体填充值一个DataSet中,然后部分现实(DataAdapter Fill重载)
DataGridView
BindingNavigate
bindingSource:将DataGridView的数据源于BindingNavicat关联


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;namespace _11_DataGridView分页显示
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private SqlDataAdapter pageDA;private DataSet pageDS = new DataSet();private int startval = 0; //起始值private int valPerPage = 3;private int toalValNumber;  //总条数private int currentPage = 1;private void Form1_Load(object sender, EventArgs e){dataGridView1.AllowUserToAddRows = false;string constr = "Server=.;user=sa;pwd=darly;database=csharpzx";SqlConnection mycon = new SqlConnection(constr);try{mycon.Open();string sql = "select * from mytable";pageDA = new SqlDataAdapter(sql, mycon);pageDA.Fill(pageDS, "mytable");toalValNumber = pageDS.Tables[0].Rows.Count;//总页数计算int totalPageNumber = (toalValNumber % valPerPage == 0) ? (toalValNumber / valPerPage) : (toalValNumber / valPerPage + 1); //三目表达式toolStripLabel1.Text = "/" + totalPageNumber;LoadData();}catch (Exception ex){MessageBox.Show(ex.Message);}finally{mycon.Close();}}private void LoadData(){currentPage = startval / valPerPage + 1;   //计算初始当前页,初始值为0,故首次打开显示第一页数据toolStripTextBox1.Text = currentPage.ToString();pageDS.Clear(); //将数据集清空pageDA.Fill(pageDS, startval, valPerPage, "mytable");bindingSource1.DataSource = pageDS.Tables[0];          //bindingSource1绑定数据源bindingNavigator1.BindingSource = bindingSource1;      //bindingNavigator1绑定数据源dataGridView1.DataSource = bindingSource1;}private void bindingNavigator1_ItemClicked(object sender, ToolStripItemClickedEventArgs e){if (e.ClickedItem.Text == "上一页"){startval = startval - valPerPage;if (startval < 0){MessageBox.Show("已经是第一页");startval = 0;return;}}if (e.ClickedItem.Text == "下一页"){startval = startval + valPerPage;if(startval>toalValNumber){MessageBox.Show("已经是最后一页");startval = startval - valPerPage;return;}                }LoadData();}}
}

12、DataGridView单元格自动填充

在DataGridView单元各种,当输入指定字符时,自动完成填充
通过TExBox实现 TextBox有以下两个属性

  • AutoCompleteMode //默认没有自动填充模式

    • AutoCompleteMode.Suggest; //需要将自动填充模式改为建议模式,建议模式需要下面的属性提供具体内容
  • AutoCompleteSource
    • AutoCompleteSource.CustomSource //自动填充数据源设置为客户定义的数据源,再定义一个数据源

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace _13_DataGridView自动填充单元格
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){// TODO: 这行代码将数据加载到表“csharpzxDataSet.mytable”中。您可以根据需要移动或删除它。this.mytableTableAdapter.Fill(this.csharpzxDataSet.mytable);}private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e){string titleText = dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].HeaderText;if(titleText.Equals("department")){TextBox autoText = e.Control as TextBox;if(autoText !=null){autoText.AutoCompleteMode = AutoCompleteMode.Suggest;autoText.AutoCompleteSource = AutoCompleteSource.CustomSource;AutoCompleteStringCollection dataCollection = new AutoCompleteStringCollection();dataCollection.Add("开发部");dataCollection.Add("技术部");dataCollection.Add("测试部");autoText.AutoCompleteCustomSource = dataCollection;}}}}
}

本章小结及任务实施

创建职员信息表:添加姓名、性别、工资
绘制行号,选中行显示为蓝色
其他行就显示不同颜色
分页显示

using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;namespace _14__任务实施
{public partial class Form1 : Form{public Form1(){InitializeComponent();}int valToalNum = 500; //产生的总条数DataTable dtable;int valPerPage = 15;  //每页条数int pageNum = 0;   //总页数int pageCurrent = 1; //当前页序号int valCurrent = 0;   //当前条数int valstartCurrent = 0;  //当前页起始条数int valEndIndex = 0; //当前页终止条数private void dtGenerate()    //数据产生方法{dtable = new DataTable("ClerkSalary");dtable.Columns.Add("姓名", Type.GetType("System.String"));dtable.Columns.Add("性别", Type.GetType("System.String"));dtable.Columns.Add("工资", Type.GetType("System.Int32"));String familyName = "赵钱孙李周吴郑王冯陈诸卫蒋沈崔胡刘";string lastName = @"姓名汉语词语人的姓氏和名字名字是人类为区分个体给每
个人特定的名称符号是通过语言文字信息区别人群个体
差异的标志由于有了姓名人类才能正常有序地交往因此每个人都有一个属于自己的姓名";string gender = "男女";Random rd = new Random();for (int i = 0; i < valToalNum; i++){//增加行方式一string name = familyName[rd.Next(0, familyName.Length)].ToString() + lastName[rd.Next(0, lastName.Length)].ToString() + lastName[rd.Next(0, lastName.Length)].ToString();string Gender = gender[rd.Next(0, gender.Length)].ToString();int salary = rd.Next(1800, 10000);dtable.Rows.Add(new object[] { name, Gender, salary });//增加行方式二//DataRow dr = dtable.NewRow();//dr[0] = familyName[rd.Next(0, familyName.Length)].ToString() + lastName[rd.Next(0, lastName.Length)].ToString() + lastName[rd.Next(0, lastName.Length)].ToString();//dr[1] = gender[rd.Next(0, gender.Length)].ToString();//dr[2] = rd.Next(1800, 1000);//dtable.Rows.Add(dr);}}private void loadData()    //加载当前页的数据{DataTable dtTemp = dtable.Clone();if (pageCurrent == pageNum) valEndIndex = valToalNum - 1; //只有一页else valEndIndex = pageCurrent * valPerPage - 1;  //不止一页valstartCurrent = valCurrent;toolStripTextBox1.Text = pageCurrent.ToString();toolStripLabel1.Text = "/" + Convert.ToString(pageNum);//从原数据表中读取当前数据for (int i = valstartCurrent; i < valEndIndex; i++){dtTemp.ImportRow(dtable.Rows[i]);   //将响应航导入到临时表中valCurrent++;}//绑定数据源bindingSource1.DataSource = dtTemp;bindingNavigator1.BindingSource = bindingSource1;dataGridView1.DataSource = bindingSource1;//奇偶行显示不同的颜色dataGridView1.RowsDefaultCellStyle.BackColor = Color.Pink;dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Violet;}private void Form1_Load(object sender, EventArgs e){try{dtGenerate();//MessageBox.Show("数据生产完成");pageNum = (valToalNum % valPerPage == 0) ? (valToalNum / valPerPage) : (valToalNum / valPerPage + 1);loadData();dataGridView1.ReadOnly = true;dataGridView1.AllowUserToAddRows = false;}catch (Exception ex){MessageBox.Show(ex.Message.ToString());                }}private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e){Rectangle myrec = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dataGridView1.RowHeadersWidth, e.RowBounds.Height);//TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dataGridView1.RowHeadersDefaultCellStyle.Font, myrec, dataGridView1.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1+valstartCurrent).ToString(), dataGridView1.RowHeadersDefaultCellStyle.Font, myrec, dataGridView1.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);if (e.RowIndex >= dataGridView1.Rows.Count - 1) return;  //如果选中的行是一个新行(待添加内容)不进行操作,直接返回Color oldForeColor = new Color();Color oldBackColor = new Color();// object是所有类的基类, var是可以是所有变量类型,不知道变量类型时,可以声明为var格式var row = dataGridView1.Rows[e.RowIndex];    //操作的行if (row == dataGridView1.CurrentRow)   //判断操作行与当前行是不是一行,如果一样代表是首次操作,所以要绘制不同样式{if (row.DefaultCellStyle.ForeColor != Color.White) {oldForeColor = row.DefaultCellStyle.ForeColor;  // 存储现在的前景色row.DefaultCellStyle.ForeColor = Color.White;   // 设置前景色为白色}if (row.DefaultCellStyle.BackColor != Color.Blue){oldBackColor = row.DefaultCellStyle.BackColor;row.DefaultCellStyle.BackColor = Color.Blue;}}else{row.DefaultCellStyle.ForeColor = oldForeColor;row.DefaultCellStyle.BackColor = oldBackColor;}}private void bindingNavigator1_ItemClicked(object sender, ToolStripItemClickedEventArgs e){if (e.ClickedItem.Text == "上一页"){pageCurrent--;if (pageCurrent <= 0){MessageBox.Show("已经是最后一页");pageCurrent++;return;}else{valCurrent = valPerPage * (pageCurrent - 1);}loadData();}if(e.ClickedItem.Text=="下一页"){pageCurrent++;if(pageCurrent>pageNum){pageCurrent--;MessageBox.Show("已经是最后一页");return;}else{valCurrent = valPerPage * (pageCurrent - 1);}loadData();}}}
}

十六、C# 表格数据控件相关推荐

  1. SAP UI5 应用开发教程之五十六 - SAP UI5 树控件(tree)的开发试读版

    一套适合 SAP UI5 初学者循序渐进的学习教程 教程目录 SAP UI5 本地开发环境的搭建 SAP UI5 应用开发教程之一:Hello World SAP UI5 应用开发教程之二:SAP U ...

  2. SAP UI5 应用开发教程之五十六 - SAP UI5 树控件(tree)的开发

    本步骤我们来学习用 SAP UI5 来显示一种在计算机应用程序开发领域非常有用的数据结构:树. 本步骤的实现源代码:https://github.com/wangzixi-diablo/ui5-tut ...

  3. Android开发笔记(三十六)展示类控件

    View/ViewGroup View是单个视图,所有的控件类都是从它派生出来:而ViewGroup是个视图组织,所有的布局视图类都是从它派生出来.由于View和ViewGroup是基类,因此很少会直 ...

  4. vb数据库编程(二)--数据控件

    什么是绑定控件?绑定控件指的是一个窗体上的"对象",由于创建它的那个控件设置了显示数据库信息方面的功能,通过设置对象的datasource(数据源)属性和datafield(数据字 ...

  5. ASP.NET-----Repeater数据控件的用法总结

    一.Repeater控件的用法流程及实例: 1.首先建立一个网站,新建一个网页index.aspx. 2.添加或者建立APP_Data数据文件,然后将用到的数据库文件放到APP_Data文件夹中. 3 ...

  6. Gridview数据控件的七种字段类型

    9.8  数据控件的七种字段类型(Fields Type)的应用 GridView共支持七种字段类型,字段原本应该叫"Column"比较恰当,但ASP.NET 2.0却采用另一个名 ...

  7. ASP.NET的五大数据控件分析

    ASP.NET 数据控件:GridView,DataList,Repeater ,DetailsView,FormView. ASP.NET 数据控件综述: 1.前3个用于呈现多条记录,后面2个用于呈 ...

  8. aspx repeater 用法_ASP.NET-----Repeater数据控件的用法总结

    一.Repeater控件的用法流程及实例: 1.首先建立一个网站,新建一个网页index.aspx. 2.添加或者建立APP_Data数据文件,然后将用到的数据库文件放到APP_Data文件夹中. 3 ...

  9. asp.net DataGridTree表格树控件 下拉树 DropTree c# .net

    1.下拉树 DropTree c# .net 下拉树实现原理 输出json到客户端 客户端实现动态加载 中间不会和服务端交互 数据量支持上 经测试 几千 还是很快的 本下拉树控件是用c#+js树实现 ...

最新文章

  1. DBA入门之路:由浅入深的总结学习法
  2. 网页设计过程中一般命名规则
  3. lazyload延迟加载组件
  4. 中石油训练赛 - Racing Gems(最长不下降子序列)
  5. Centos6.4 编译安装 nginx php
  6. iOS 9.0系统策略更新
  7. CSS3背景渐变。。。
  8. 违背基本假设的几种情况——自相关性(R语言)
  9. origin中文版散点图拟合曲线_Origin9绘图时对一组散点图中的不同部分分别进行线性拟合的方法...
  10. 课后作业3:软件分析与用户体验分析
  11. bluefish开发php,Bluefish—优秀的Linux下HTML编辑器
  12. ESP8266连得上WIFI却连不上手机热点
  13. Android 打印Log语句
  14. 如何查看笔记本电脑的型号?
  15. 每天一点英文(记录)
  16. db(德邦快递单号查询)
  17. 北京可视化暑期学校日志——Day1
  18. 【Unity连载】斗兽棋-棋类游戏开发演示(2)
  19. 朋友圈都在说的央行数字货币,究竟跟你有什么关系
  20. 【问题】Ubuntu20.04桌面某些图标不显示

热门文章

  1. 微信号以及手机号的正则表达式
  2. POST请求与GET请求
  3. 模拟jd快递单号查询
  4. C++实现生产者和消费者模型
  5. 【Angular】Angular8开发拼多多——Angular CLI的环境配置
  6. 只需要努力,其他的都交给时光----三级网络学习总结
  7. 页面应用访问统计 - GA [Google Analytics]
  8. bs4主要知识点介绍及实例解析---利用bs4爬取伯乐在线(分别存储在数据库和xls表中)
  9. which的用法总结c语言,which的用法
  10. git本地分支 远程分支简单操作(后续更新)