日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#实例:datagridview单元格合并

發(fā)布時間:2023/12/4 C# 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#实例:datagridview单元格合并 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?這是替C#微信交流群群友做的一個小實例,目的就是在datagridview選擇對應行以后,點擊button后獲取對應行的ip,并執(zhí)行相應的操作,其實我覺得這樣的話button沒必要非放置到datagridview里面的!但是為了滿足群友的需求,還是這么做了。

先看一下運行效果:

1. DataGridView 添加一列checkbox

DataGridViewCheckBoxColumn newColumn = new DataGridViewCheckBoxColumn();
newColumn.HeaderText = "選擇";
dataGridView1.Columns.Add(newColumn);

這樣添加的列是放在最后一列,也許你希望它在其它列,例如第二列,那么可以:
dataGridView1.Columns.Insert(1, newColumn);

2.?DataGridView 添加一個button

? ? ? ? ? ? btn1.Name = "btnRun";
??????????? btn1.Text = "Run";
??????????? btn1.Visible = true;
??????????? btn1.Location = new Point(550, 80);
??????????? btn1.Size = new Size(80, 50);
??????????? btn1.Parent = this;
??????????? btn1.Click += new EventHandler(btn1_Click);
??????????? //this.Controls.Add(btn1);
??????????? dataGridView1.Controls.Add(btn1);

3. 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 WindowsFormsApp28 {public partial class Form1 : Form{public Form1(){InitializeComponent();}Button btn1 = new Button();private void Form1_Load(object sender, EventArgs e){DataTable dt = new DataTable();dt.Columns.Add("IP");dt.Columns.Add("Option");dt.Columns.Add("button");dt.Rows.Add("192.168.1.10", null, null);dt.Rows.Add("192.168.1.11", null, null);dt.Rows.Add("192.168.1.12", null, null);dt.Rows.Add("192.168.1.13", null, null);dt.Rows.Add("192.168.1.14", null, null);dt.Rows.Add("192.168.1.15", null, null);dt.Rows.Add("192.168.1.16", null, null);dt.Rows.Add("192.168.1.17", null, null);dt.Rows.Add("192.168.1.18", null, null);dt.Rows.Add("192.168.1.19", null, null);dataGridView1.DataSource = dt;//var list = new List<Object>();//list.Add(new { IP = "192.168.1.10", Option = "null", button = "null" });//list.Add(new { IP = "192.168.1.11", Option = "null", button = "null" });//list.Add(new { IP = "192.168.1.12", Option = "null", button = "null" });//list.Add(new { IP = "192.168.1.13", Option = "null", button = "null" });//list.Add(new { IP = "192.168.1.14", Option = "null", button = "null" });//list.Add(new { IP = "192.168.1.15", Option = "null", button = "null" });//dataGridView1.DataSource = list;DataGridViewCheckBoxColumn newColumn1 = new DataGridViewCheckBoxColumn();newColumn1.HeaderText = "選擇";//dataGridView1.Columns.Add(newColumn);dataGridView1.Columns.Insert(3, newColumn1);DataGridViewButtonColumn newColumn2 = new DataGridViewButtonColumn();newColumn2.HeaderText = "控件";//dataGridView1.Columns.Add(newColumn);dataGridView1.Columns.Insert(4, newColumn2);dt.Columns.Add("action");dataGridView1.Rows[0].Cells[0].Value = true;//dataGridView1.Rows[0].Cells[1].Value = true;btn1.Name = "btnRun";btn1.Text = "Run";btn1.Visible = true;btn1.Location = new Point(550, 80);btn1.Size = new Size(80, 50);btn1.Parent = this;btn1.Click += new EventHandler(btn1_Click);//this.Controls.Add(btn1);dataGridView1.Controls.Add(btn1);}private void btn1_Click(object sender, EventArgs e){// MessageBox.Show("123");for (int i = 0; i < dataGridView1.Rows.Count; i++){string otherValue = dataGridView1.Rows[i].Cells[0].EditedFormattedValue.ToString();if (otherValue == "True")MessageBox.Show(dataGridView1.Rows[i].Cells[2].Value.ToString());}}private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e){if (e.ColumnIndex == dataGridView1.Columns[1].Index)MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString());}/// <summary>/// 將當前單元格中的更改提交到數(shù)據(jù)緩存,但不結束編輯模式,及時獲得其狀態(tài)是選中還是未選中 /// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e){if (dataGridView1.IsCurrentCellDirty){dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);}}private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e){try{if (dataGridView1.Rows.Count > 0){int rowIndex = dataGridView1.CurrentCell.RowIndex;int colIndex = dataGridView1.CurrentCell.ColumnIndex;if (colIndex == 0) //第一列{string _selectValue = dataGridView1.CurrentCell.EditedFormattedValue.ToString();if (_selectValue == "True"){for (int i = 0; i < dataGridView1.Rows.Count; i++){if (i != rowIndex){string otherValue = dataGridView1.Rows[i].Cells[0].EditedFormattedValue.ToString();if (otherValue == "True"){((DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells[0]).Value = false;}}}}}}}catch (Exception ex){ }}private void button1_Click(object sender, EventArgs e){}private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e){// 對第5列相同單元格進行合并 if (e.ColumnIndex == 5 && e.RowIndex != -1){using(Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),backColorBrush = new SolidBrush(e.CellStyle.BackColor)){using (Pen gridLinePen = new Pen(gridBrush)){// 清除單元格 e.Graphics.FillRectangle(backColorBrush, e.CellBounds);// 畫 Grid 邊線(僅畫單元格的底邊線和右邊線) // 如果下一行和當前行的數(shù)據(jù)不同,則在當前的單元格畫一條底邊線 if (e.RowIndex < dataGridView1.Rows.Count - 1 &&dataGridView1.Rows[e.RowIndex ].Cells[e.ColumnIndex].Value.ToString() !=e.Value.ToString())e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left + 2,e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,e.CellBounds.Bottom - 1);//畫最后一條記錄的底線 if (e.RowIndex == dataGridView1.Rows.Count - 1)e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left + 2,e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,e.CellBounds.Bottom - 1);// 畫右邊線 e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,e.CellBounds.Top, e.CellBounds.Right - 1,e.CellBounds.Bottom);// 畫左邊線 e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left ,e.CellBounds.Top, e.CellBounds.Left ,e.CellBounds.Bottom);// 畫(填寫)單元格內容,相同的內容的單元格只填寫第一個 if (e.Value != null){if (e.RowIndex > 0 &&dataGridView1.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString() ==e.Value.ToString()){}else{//e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,//Brushes.Black, e.CellBounds.X + 2,//e.CellBounds.Y + 5, StringFormat.GenericDefault);}}e.Handled = true;}}}}} } 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結

以上是生活随笔為你收集整理的C#实例:datagridview单元格合并的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。