c#dataGridView设置单元格颜色隐藏行列-禁止修改高度宽度-设置行列宽度-取消选中功能
c#dataGridView设置单元格颜色隐藏行列-禁止修改高度宽度-设置行列宽度-取消选中功能
private void Form1_Load(object sender, EventArgs e) { Console.WriteLine(DateTime.Now.ToString("yyyyMMddHHmmssfff")); serialPort = new CSerialPort();//通信串口----------------- dataGridView1.RowHeadersVisible = false; //隐藏行头部 dataGridView1.ColumnHeadersVisible = false; //列行头部 dataGridView1.AllowUserToResizeColumns = false; // 禁止用户改变DataGridView的所有列的列宽 dataGridView1.AllowUserToResizeRows = false;//禁止用户改变DataGridView所有行的行高 // 禁止用户改变列头的高度 dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing; // 禁止用户改变列头的宽度 dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing; dataGridView1.AllowUserToAddRows = false;//不显示空白新增行 //dataGridView1. DataTable dt2; dt2 = new DataTable("",""); dt2.Columns.Add("",typeof(string)); dt2.Columns.Add("", typeof(string)); dt2.Columns.Add("", typeof(string)); dt2.Columns.Add("", typeof(string)); dt2.Columns.Add("", typeof(string)); dt2.Columns.Add("", typeof(string)); dt2.Columns.Add("", typeof(string)); dt2.Rows.Add(new object[] {"xxx(3L)","","", "", "", "", "" }); dt2.Rows.Add(new object[] { "xxxx(2L)", "", "", "", "", "", "" }); dt2.Rows.Add(new object[] { "xxxx(1L)", "", "", "", "", "", "" }); // List<string> results = dt2.AsEnumerable().Select(x => x.Field<string>("工号")).ToList(); //foreach (var i in results) { // Console.WriteLine(i); } dataGridView1.DataSource = dt2; dataGridView1.Rows[0].Cells[0].Style.BackColor = Color.Aqua; dataGridView1.Rows[1].Cells[0].Style.BackColor = Color.LightGray; dataGridView1.Rows[2].Cells[0].Style.BackColor = Color.Aqua; dataGridView1.Columns[0].Width = 120; dataGridView1.Columns[1].Width = 30; dataGridView1.Columns[2].Width = 30; dataGridView1.Columns[3].Width = 30; dataGridView1.Columns[4].Width = 30; dataGridView1.Columns[5].Width = 30; dataGridView1.Columns[6].Width = 30; dataGridView1.Rows[0].Cells[1].Style.BackColor = Color.Lime; dataGridView1.Rows[0].Cells[2].Style.BackColor = Color.Lime; dataGridView1.Rows[1].Cells[3].Style.BackColor = Color.Lime; dataGridView1.Rows[2].Cells[5].Style.BackColor = Color.Lime; dataGridView1.Rows[2].Cells[6].Style.BackColor = Color.Lime; Console.WriteLine("dt3 data"); DataTable dt3; dt3 = (DataTable)dataGridView1.DataSource; for(int i=0;i<dt3.Columns.Count;++i) { Console.Write(dt3.Columns[i]+" "); //输出不换行 } Console.WriteLine(""); //Console.WriteLine("换行"); for (int j=0;j<dt3.Rows.Count;++j) { // Console.Write(dataRow["姓名"].ToString()); } foreach(DataRow dataRow in dt3.Rows) { //string aa = dataRow["姓名"].ToString(); // string aa2 = dataRow["工号"].ToString(); // string aa3 = dataRow["地址2"].ToString(); // Console.WriteLine(aa+" "+ aa2+" "+aa3); } // dt3.Rows[0]["姓名"] = "力工"; // dt3.Rows[0]["工号"] = "M888"; // dt3.Rows[0]["地址2"] = "柳树"; Console.WriteLine(""); Console.WriteLine("修改第一行数据后"); Console.WriteLine(""); foreach (DataRow dataRow in dt3.Rows) { // string aa = dataRow["姓名"].ToString(); // string aa2 = dataRow["工号"].ToString(); //string aa3 = dataRow["地址2"].ToString(); // Console.WriteLine(aa + " " + aa2 + " " + aa3); } //dataGridView1.Rows.RemoveAt(1); }
取消选中:
private void dataGridView_front_section_storage_07_SelectionChanged_1(object sender, EventArgs e)
{
dataGridView_front_section_storage_07.ClearSelection();
}
欢迎讨论,相互学习。
cdtxw@foxmail.com