zoukankan      html  css  js  c++  java
  • c# winform 关于DataGridView的一些操作

    设置字段名 
    设置字段值 
    设定单元格表示 Error图标 
    设定当前单元格 
    取得当前单元格内容 
    取得当前单元格的列 Index 
    取得当前单元格的行 Index 
    向下一行 
    向上一行 
    取消 DataGridView1 为只读 
    设置 DataGridView1 为只读 
    设置 DataGridView1 的第2列整列单元格为只读并变色 
    设置 DataGridView1 的第3行整行单元格为只读并变色 
    设置 DataGridView1 的[0,0]单元格为只读并变色 
    设置 DataGridView1 的第2列整列单元格为只读并变色 
    设置 DataGridView1 的第3行整行单元格为只读并变色 
    设置 DataGridView1 的[0,0]单元格为只读并变色 
    设置用户不能手动给 DataGridView1 添加新行 
    设置用户可以手动给 DataGridView1 添加新行 
    禁止DataGridView1的行删除操作。 
    允许DataGridView1的行删除操作。 
    DataGridView1的第一列隐藏 
    DataGridView1的第一行隐藏 
    DataGridView1的第一列显示 
    DataGridView1的第一行显示 
    列头隐藏 
    行头隐藏 
    列头显示 
    行头显示 
    删除名为"Column1"的列 
    删除第四列 
    删除第三行 
    禁止用户改变DataGridView1的所有列的列宽 
    禁止用户改变DataGridView1的所有行的行高 
    允许用户改变DataGridView1的所有列的列宽 
    允许用户改变DataGridView1的所有行的行高 
    禁止用户改变DataGridView1的第一列的列宽 
    禁止用户改变DataGridView1的第一列的行宽 
    第一列的最小列宽设定为 100 
    第一行的最小行高设定为 50 
    禁止用户改变列头的高度 
    禁止用户改变行头的宽度 
    设定包括Header和所有单元格的列宽自动调整 
    设定包括Header和所有单元格的行高自动调整 
    第一列自动调整 
    设定列头的宽度可以自动调整 
    设定行头的宽度可以自动调整 
    让 DataGridView1 的所有列宽自动调整一下。 
    让 DataGridView1 的第三列的列宽自动调整一下。 
    让 DataGridView1 的所有行高自动调整一下。 
    让 DataGridView1 的第一行的行高自动调整一下。 
    列头高度自动调整 
    行头宽度自动调整 
    DataGridView1的左侧2列固定 
    DataGridView1 的上3行固定 
    改变DataGridView1的第一列列头内容 
    改变DataGridView1的第一行行头内容 
    改变DataGridView1的左上头部单元内容 
    改变DataGridView1的第二列列头内容 
    当前单元格是否选择的判断 
    获取剪切板的内容,并按行分割 
    是否是列头 
    按 Tab 分割数据 
    行头设定 
    单元格内容设定 
    DataGridView的行索引+1 
    设定单元格的ToolTip内容 
    设定列头的单元格的ToolTip内容 
    设定行头的单元格的ToolTip内容 
    DataGridView 的 ContextMenuStrip 设定 
    列的 ContextMenuStrip 设定 
    列头的 ContextMenuStrip 设定 
    行的 ContextMenuStrip 设定 
    单元格的 ContextMenuStrip 设定 
    列头的ContextMenuStrip设定 
    行头的ContextMenuStrip设定 
    如果单元格值是整数时 
    当"Column1"列是Bool型且为True时、设定其的ContextMenuStrip 
    如果单元格是“Column1”列的单元格 
    将单元格值改为大写 
    应用该Format,Format完毕。 
    单元格列为“Column2”时 
    将单元格值设为大写 
    解析完毕 
    设定单元格的默认值 
    单元格值为负整数时,Error图标被表示。 
    行的错误提示的设定 
    验证通过的话,则清空行的错误提示 

    C#代码 
      1. using System;  
      2. using System.Collections.Generic;  
      3. using System.ComponentModel;  
      4. using System.Data;  
      5. using System.Drawing;  
      6. using System.Text;  
      7. using System.Windows.Forms;  
      8.   
      9.   
      10.   
      11. using System.Data.OracleClient;  
      12.   
      13.      
      14. using System.IO;    
      15. using System.Data.OleDb;   
      16.   
      17. namespace TestMouseMove  
      18. {  
      19.     public partial class Form3 : Form  
      20.     {  
      21.         public Form3()  
      22.         {  
      23.             InitializeComponent();  
      24.             dgvLoad();  
      25.         }  
      26.   
      27.         public void dgvLoad()  
      28.         {  
      29.             this.DataGridView1.Columns[0].HeaderCell.Value = "姓名";  
      30.             this.DataGridView1.Columns[1].HeaderCell.Value = "字段2";  
      31.             this.DataGridView1.Columns[2].HeaderCell.Value = "字段3";  
      32.             this.DataGridView1.Columns[3].HeaderCell.Value = "字段4";  
      33.             this.DataGridView1.Columns[4].HeaderCell.Value = "字段5";  
      34.   
      35.             this.DataGridView1.Rows.Add("黑色头发", "1行2列", "1行3列", "1行4列", "1行5列");  
      36.             this.DataGridView1.Rows.Add("紫色头发", "2行2列aaaa", "2行3列", "2行4列", "2行5列");  
      37.             this.DataGridView1.Rows.Add("蓝色头发", "3行2列", "3行3列", "3行4列", "3行5列");  
      38.             this.DataGridView1.Rows.Add("红色头发", "4行2列", "4行3列", "4行4列", "4行5列");  
      39.             this.DataGridView1.Rows.Add("棕色头发", "5行2列", "5行3列", "5行4列", "5行5列");  
      40.   
      41.             DataGridView1.ShowCellErrors = true;  
      42.             //设定 (0, 0) 的单元格表示 Error图标  
      43.             DataGridView1[3, 3].ErrorText = "这是单元格提示错误信息,也可以再行头显示这个错误提示信息";  
      44.   
      45.             //设定第4行(Index=3)的行头显示Error图标  
      46.             //DataGridView1.Rows[0].ErrorText = "不能输入负值。";  
      47.         }  
      48.   
      49.         private void button1_Click(object sender, EventArgs e)  
      50.         {  
      51.             // 设定 (1, 2) 为当前单元格  
      52.             DataGridView1.CurrentCell = DataGridView1[1, 2];              
      53.         }  
      54.   
      55.         private void button2_Click(object sender, EventArgs e)  
      56.         {  
      57.             // 取得当前单元格内容  
      58.             MessageBox.Show(DataGridView1.CurrentCell.Value.ToString());  
      59.             // 取得当前单元格的列 Index  
      60.             MessageBox.Show(DataGridView1.CurrentCell.ColumnIndex.ToString());  
      61.             // 取得当前单元格的行 Index  
      62.             MessageBox.Show(DataGridView1.CurrentCell.RowIndex.ToString());  
      63.         }  
      64.   
      65.         /// <summary>  
      66.         /// 向下遍历  
      67.         /// </summary>  
      68.         /// <param name="sender"></param>  
      69.         /// <param name="e"></param>  
      70.         private void button3_Click(object sender, EventArgs e)  
      71.         {  
      72.             int row = this.DataGridView1.CurrentRow.Index + 1;  
      73.             if (row > this.DataGridView1.RowCount - 1)  
      74.                 row = 0;  
      75.             this.DataGridView1.CurrentCell = this.DataGridView1[0, row];  
      76.         }  
      77.   
      78.         /// <summary>  
      79.         /// 向上遍历  
      80.         /// </summary>  
      81.         /// <param name="sender"></param>  
      82.         /// <param name="e"></param>  
      83.         private void button4_Click(object sender, EventArgs e)  
      84.         {  
      85.             int row = this.DataGridView1.CurrentRow.Index - 1;  
      86.             if (row < 0)  
      87.                 row = this.DataGridView1.RowCount - 1;  
      88.             this.DataGridView1.CurrentCell = this.DataGridView1[0, row];  
      89.         }  
      90.   
      91.         private void button5_Click(object sender, EventArgs e)  
      92.         {  
      93.             if (DataGridView1.ReadOnly)  
      94.             {  
      95.                 // 取消 DataGridView1 为只读  
      96.                 DataGridView1.ReadOnly = false;  
      97.                 DataGridView1.DefaultCellStyle.BackColor = Color.White;  
      98.             }  
      99.             else  
      100.             {  
      101.                 // 设置 DataGridView1 为只读  
      102.                 DataGridView1.ReadOnly = true;  
      103.                 DataGridView1.DefaultCellStyle.BackColor = Color.Gainsboro;  
      104.             }  
      105.         }  
      106.   
      107.         private void button6_Click(object sender, EventArgs e)  
      108.         {  
      109.             if (DataGridView1.Columns[1].ReadOnly)  
      110.             {  
      111.                 // 设置 DataGridView1 的第2列整列单元格为只读并变色  
      112.                 DataGridView1.Columns[1].ReadOnly = false;  
      113.                 DataGridView1.Columns[1].DefaultCellStyle.BackColor = Color.White;  
      114.                 // 设置 DataGridView1 的第3行整行单元格为只读并变色  
      115.                 DataGridView1.Rows[2].ReadOnly = false;  
      116.                 DataGridView1.Rows[2].DefaultCellStyle.BackColor = Color.White;  
      117.                 // 设置 DataGridView1 的[0,0]单元格为只读并变色  
      118.                 DataGridView1[0, 0].ReadOnly = false;  
      119.                 DataGridView1.Rows[0].Cells[0].Style.BackColor = Color.White;   
      120.             }  
      121.             else  
      122.             {  
      123.                 // 设置 DataGridView1 的第2列整列单元格为只读并变色  
      124.                 DataGridView1.Columns[1].ReadOnly = true;  
      125.                 DataGridView1.Columns[1].DefaultCellStyle.BackColor = Color.Gainsboro;  
      126.                 // 设置 DataGridView1 的第3行整行单元格为只读并变色  
      127.                 DataGridView1.Rows[2].ReadOnly = true;  
      128.                 DataGridView1.Rows[2].DefaultCellStyle.BackColor = Color.Gainsboro;  
      129.                 // 设置 DataGridView1 的[0,0]单元格为只读并变色  
      130.                 DataGridView1[0, 0].ReadOnly = true;  
      131.                 DataGridView1.Rows[0].Cells[0].Style.BackColor = Color.Gainsboro;   
      132.             }  
      133.         }  
      134.   
      135.         private void button7_Click(object sender, EventArgs e)  
      136.         {  
      137.             if (DataGridView1.EditMode.Equals(DataGridViewEditMode.EditOnEnter))  
      138.             {  
      139.                 DataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;  
      140.                 DataGridView1.DefaultCellStyle.BackColor = Color.Red;  
      141.             }  
      142.             else  
      143.             {  
      144.                 DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;  
      145.                 DataGridView1.DefaultCellStyle.BackColor = Color.White;  
      146.             }  
      147.         }  
      148.   
      149.         private void button8_Click(object sender, EventArgs e)  
      150.         {  
      151.             DataGridView1.Rows[DataGridView1.CurrentCell.RowIndex].Cells[DataGridView1.CurrentCell.ColumnIndex].Style.BackColor = Color.White;  
      152.             DataGridView1.BeginEdit(true);  
      153.         }  
      154.   
      155.         private void button9_Click(object sender, EventArgs e)  
      156.         {  
      157.             if (DataGridView1.AllowUserToAddRows)  
      158.             {  
      159.                 // 设置用户不能手动给 DataGridView1 添加新行  
      160.                 DataGridView1.AllowUserToAddRows = false;  
      161.             }  
      162.             else  
      163.             {  
      164.                 // 设置用户可以手动给 DataGridView1 添加新行  
      165.                 DataGridView1.AllowUserToAddRows = true;  
      166.             }  
      167.         }  
      168.   
      169.         private void button10_Click(object sender, EventArgs e)  
      170.         {  
      171.             if (DataGridView1.AllowUserToDeleteRows)  
      172.             {  
      173.                 // 禁止DataGridView1的行删除操作。  
      174.                 DataGridView1.AllowUserToDeleteRows = false;  
      175.                 DataGridView1.DefaultCellStyle.BackColor = Color.Wheat;  
      176.             }  
      177.             else  
      178.             {  
      179.                 // 允许DataGridView1的行删除操作。  
      180.                 DataGridView1.AllowUserToDeleteRows = true;  
      181.                 DataGridView1.DefaultCellStyle.BackColor = Color.White;  
      182.             }  
      183.         }  
      184.   
      185.         private void button11_Click(object sender, EventArgs e)  
      186.         {  
      187.             if (DataGridView1.Columns[0].Visible)  
      188.             {  
      189.                 // DataGridView1的第一列隐藏  
      190.                 DataGridView1.Columns[0].Visible = false;  
      191.                 // DataGridView1的第一行隐藏  
      192.                 DataGridView1.Rows[0].Visible = false;  
      193.             }  
      194.             else  
      195.             {  
      196.                 // DataGridView1的第一列显示  
      197.                 DataGridView1.Columns[0].Visible = true;  
      198.                 // DataGridView1的第一行显示  
      199.                 DataGridView1.Rows[0].Visible = true;  
      200.             }  
      201.         }  
      202.   
      203.         private void button12_Click(object sender, EventArgs e)  
      204.         {  
      205.             if (DataGridView1.ColumnHeadersVisible)  
      206.             {  
      207.                 // 列头隐藏  
      208.                 DataGridView1.ColumnHeadersVisible = false;  
      209.                 // 行头隐藏  
      210.                 DataGridView1.RowHeadersVisible = false;  
      211.             }  
      212.             else  
      213.             {  
      214.                 // 列头显示  
      215.                 DataGridView1.ColumnHeadersVisible = true;  
      216.                 // 行头显示  
      217.                 DataGridView1.RowHeadersVisible = true;  
      218.             }  
      219.         }  
      220.   
      221.         private void button13_Click(object sender, EventArgs e)  
      222.         {  
      223.             // 删除名为"Column1"的列  
      224.             DataGridView1.Columns.Remove("Column1");  
      225.             // 删除第四列  
      226.             DataGridView1.Columns.RemoveAt(2);  
      227.             // 删除第三行  
      228.             DataGridView1.Rows.RemoveAt(2);  
      229.         }  
      230.   
      231.         private void button14_Click(object sender, EventArgs e)  
      232.         {  
      233.             foreach (DataGridViewRow r in DataGridView1.SelectedRows)  
      234.             {  
      235.                 if (!r.IsNewRow)  
      236.                 {  
      237.                     DataGridView1.Rows.Remove(r);  
      238.                 }  
      239.             }  
      240.         }  
      241.   
      242.         private void button15_Click(object sender, EventArgs e)  
      243.         {  
      244.             if (DataGridView1.AllowUserToResizeColumns)  
      245.             {  
      246.                 // 禁止用户改变DataGridView1的所有列的列宽  
      247.                 DataGridView1.AllowUserToResizeColumns = false;  
      248.                 //禁止用户改变DataGridView1的所有行的行高  
      249.                 DataGridView1.AllowUserToResizeRows = false;  
      250.             }  
      251.             else  
      252.             {  
      253.                 // 允许用户改变DataGridView1的所有列的列宽  
      254.                 DataGridView1.AllowUserToResizeColumns = true;  
      255.                 // 允许用户改变DataGridView1的所有行的行高  
      256.                 DataGridView1.AllowUserToResizeRows = true;  
      257.             }  
      258.         }  
      259.   
      260.         private void button16_Click(object sender, EventArgs e)  
      261.         {  
      262.             // 禁止用户改变DataGridView1的第一列的列宽  
      263.             DataGridView1.Columns[0].Resizable = DataGridViewTriState.False;  
      264.             // 禁止用户改变DataGridView1的第一列的行宽  
      265.             DataGridView1.Rows[0].Resizable = DataGridViewTriState.False;  
      266.         }  
      267.   
      268.         private void button17_Click(object sender, EventArgs e)  
      269.         {  
      270.             // 第一列的最小列宽设定为 100  
      271.             DataGridView1.Columns[0].MinimumWidth = 200;  
      272.             // 第一行的最小行高设定为 50  
      273.             DataGridView1.Rows[0].MinimumHeight = 50;  
      274.         }  
      275.   
      276.         private void button18_Click(object sender, EventArgs e)  
      277.         {  
      278.             // 禁止用户改变列头的高度  
      279.             DataGridView1.ColumnHeadersHeightSizeMode =  
      280.             DataGridViewColumnHeadersHeightSizeMode.DisableResizing;  
      281.             // 禁止用户改变行头的宽度  
      282.             DataGridView1.RowHeadersWidthSizeMode =  
      283.             DataGridViewRowHeadersWidthSizeMode.DisableResizing;  
      284.         }  
      285.   
      286.         private void button19_Click(object sender, EventArgs e)  
      287.         {  
      288.             // 设定包括Header和所有单元格的列宽自动调整  
      289.             DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;  
      290.             // 设定包括Header和所有单元格的行高自动调整  
      291.             DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;  
      292.         }  
      293.   
      294.         private void button20_Click(object sender, EventArgs e)  
      295.         {  
      296.             // 第一列自动调整  
      297.             DataGridView1.Columns[0].AutoSizeMode =  
      298.             DataGridViewAutoSizeColumnMode.DisplayedCells;  
      299.         }  
      300.   
      301.         private void button21_Click(object sender, EventArgs e)  
      302.         {  
      303.             // 设定列头的宽度可以自动调整  
      304.             DataGridView1.ColumnHeadersHeightSizeMode =  
      305.             DataGridViewColumnHeadersHeightSizeMode.AutoSize;  
      306.             // 设定行头的宽度可以自动调整  
      307.             DataGridView1.RowHeadersWidthSizeMode =  
      308.             DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;  
      309.         }  
      310.   
      311.         private void button22_Click(object sender, EventArgs e)  
      312.         {  
      313.             // 让 DataGridView1 的所有列宽自动调整一下。  
      314.             DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);  
      315.         }  
      316.   
      317.         private void button23_Click(object sender, EventArgs e)  
      318.         {  
      319.             // 让 DataGridView1 的第三列的列宽自动调整一下。  
      320.             DataGridView1.AutoResizeColumn(2, DataGridViewAutoSizeColumnMode.AllCells);  
      321.         }  
      322.   
      323.         private void button24_Click(object sender, EventArgs e)  
      324.         {  
      325.             // 让 DataGridView1 的所有行高自动调整一下。  
      326.             DataGridView1.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells);  
      327.             //让 DataGridView1 的第一行的行高自动调整一下。  
      328.             DataGridView1.AutoResizeRow(0, DataGridViewAutoSizeRowMode.AllCells);  
      329.         }  
      330.   
      331.         private void button25_Click(object sender, EventArgs e)  
      332.         {  
      333.             // 列头高度自动调整  
      334.             DataGridView1.AutoResizeColumnHeadersHeight();  
      335.             // 行头宽度自动调整  
      336.             DataGridView1.AutoResizeRowHeadersWidth(  
      337.             DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);  
      338.         }  
      339.   
      340.         private void button26_Click(object sender, EventArgs e)  
      341.         {  
      342.             // DataGridView1的左侧2列固定  
      343.             DataGridView1.Columns[1].Frozen = true;  
      344.             DataGridView1.Columns[2].Width = 800;  
      345.         }  
      346.   
      347.         private void button27_Click(object sender, EventArgs e)  
      348.         {  
      349.             // DataGridView1 的上3行固定  
      350.             DataGridView1.Rows[2].Frozen = true;  
      351.             DataGridView1.Rows[3].Height = 500;  
      352.         }  
      353.   
      354.         private void button28_Click(object sender, EventArgs e)  
      355.         {  
      356.             DataGridView1.AllowUserToOrderColumns = true;  
      357.         }  
      358.   
      359.         private void button29_Click(object sender, EventArgs e)  
      360.         {  
      361.             // 改变DataGridView1的第一列列头内容  
      362.             DataGridView1.Columns[0].HeaderCell.Value = "第一列";  
      363.             // 改变DataGridView1的第一行行头内容  
      364.             DataGridView1.Rows[0].HeaderCell.Value = "第一行";  
      365.             // 改变DataGridView1的左上头部单元内容  
      366.             DataGridView1.TopLeftHeaderCell.Value = "左上";  
      367.   
      368.             // 改变DataGridView1的第二列列头内容  
      369.             DataGridView1.Columns[1].HeaderText = "第二列";  
      370.         }  
      371.   
      372.         private void button30_Click(object sender, EventArgs e)  
      373.         {  
      374.             Clipboard.SetDataObject(DataGridView1.GetClipboardContent());  
      375.         }  
      376.   
      377.         private void button31_Click(object sender, EventArgs e)  
      378.         {  
      379.             //当前单元格是否选择的判断  
      380.             if (DataGridView1.CurrentCell == null)  
      381.             {  
      382.                 return;  
      383.             }  
      384.             int insertRowIndex = DataGridView1.CurrentCell.RowIndex;  
      385.             int insertColumnsIndex = DataGridView1.CurrentCell.ColumnIndex;  
      386.             // 获取剪切板的内容,并按行分割  
      387.             string pasteText = Clipboard.GetText();  
      388.             if (string.IsNullOrEmpty(pasteText))  
      389.             {  
      390.                 return;  
      391.             }  
      392.             pasteText = pasteText.Replace(" ", " ");  
      393.             pasteText = pasteText.Replace(' ', ' ');  
      394.             pasteText.TrimEnd(new char[] { ' ' });  
      395.             string[] lines = pasteText.Split(' ');  
      396.             bool isHeader = false;  
      397.             foreach (string line in lines)  
      398.             {  
      399.                 // 是否是列头  
      400.                 if (isHeader)  
      401.                 {  
      402.                     isHeader = false;  
      403.                     continue;  
      404.                 }  
      405.                 // 按 Tab 分割数据  
      406.                 string[] vals = line.Split(' ');  
      407.                 DataGridViewRow row = DataGridView1.Rows[insertRowIndex];  
      408.                 // 行头设定  
      409.                 //row.HeaderCell.Value = vals[0];  
      410.                 // 单元格内容设定  
      411.                 for (int i = 0; i < vals.Length; i++)  
      412.                 {  
      413.                     row.Cells[insertColumnsIndex].Value = vals[i];  
      414.                 }  
      415.                 // DataGridView的行索引+1  
      416.                 insertRowIndex++;  
      417.             }  
      418.         }  
      419.   
      420.         private void button32_Click(object sender, EventArgs e)  
      421.         {  
      422.             // 设定单元格的ToolTip内容  
      423.             DataGridView1[0, 0].ToolTipText = "该单元格的内容不能修改";  
      424.             // 设定列头的单元格的ToolTip内容  
      425.             DataGridView1.Columns[0].ToolTipText = "该列只能输入数字";  
      426.             // 设定行头的单元格的ToolTip内容  
      427.             DataGridView1.Rows[0].HeaderCell.ToolTipText = "该行单元格内容不能修改";  
      428.   
      429.             DataGridView1.VirtualMode = false;  
      430.             DataGridView1.ShowCellToolTips = true;  
      431.               
      432.         }  
      433.   
      434.         private void DataGridView1_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e)  
      435.         {  
      436.             MessageBox.Show(e.ToolTipText = e.ColumnIndex.ToString() + ", " + e.RowIndex.ToString());  
      437.   
      438.         }  
      439.   
      440.         private void button33_Click(object sender, EventArgs e)  
      441.         {  
      442.             // DataGridView 的 ContextMenuStrip 设定  
      443.             DataGridView1.ContextMenuStrip = this.ContextMenuStrip1;  
      444.             // 列的 ContextMenuStrip 设定  
      445.             DataGridView1.Columns[0].ContextMenuStrip = this.ContextMenuStrip2;  
      446.             // 列头的 ContextMenuStrip 设定  
      447.             DataGridView1.Columns[0].HeaderCell.ContextMenuStrip = this.ContextMenuStrip2;  
      448.             // 行的 ContextMenuStrip 设定  
      449.             DataGridView1.Rows[0].ContextMenuStrip = this.ContextMenuStrip3;  
      450.             // 单元格的 ContextMenuStrip 设定  
      451.             DataGridView1[0, 0].ContextMenuStrip = this.ContextMenuStrip4;  
      452.         }  
      453.   
      454.         private void DataGridView1_CellContextMenuStripNeeded(object sender, DataGridViewCellContextMenuStripNeededEventArgs e)  
      455.         {  
      456.             DataGridView dgv = (DataGridView)sender;  
      457.             if (e.RowIndex < 0)  
      458.             {  
      459.                 // 列头的ContextMenuStrip设定  
      460.                 e.ContextMenuStrip = this.ContextMenuStrip1;  
      461.             }  
      462.             else if (e.ColumnIndex < 0)  
      463.             {  
      464.                 // 行头的ContextMenuStrip设定  
      465.                 e.ContextMenuStrip = this.ContextMenuStrip2;  
      466.             }  
      467.             else if (dgv[e.ColumnIndex, e.RowIndex].Value is int)  
      468.             {  
      469.                 // 如果单元格值是整数时  
      470.                 e.ContextMenuStrip = this.ContextMenuStrip3;  
      471.             }  
      472.         }  
      473.   
      474.         private void DataGridView1_RowContextMenuStripNeeded(object sender, DataGridViewRowContextMenuStripNeededEventArgs e)  
      475.         {  
      476.             DataGridView dgv = (DataGridView)sender;  
      477.             // 当"Column1"列是Bool型且为True时、设定其的ContextMenuStrip  
      478.             object boolVal = dgv["Column1", e.RowIndex].Value;  
      479.             Console.WriteLine(boolVal);  
      480.             if (boolVal is bool && (bool)boolVal)  
      481.             {  
      482.                 e.ContextMenuStrip = this.ContextMenuStrip1;  
      483.             }  
      484.         }  
      485.   
      486.         private void button34_Click(object sender, EventArgs e)  
      487.         {  
      488.             DataGridView1.BorderStyle = BorderStyle.None;  
      489.         }  
      490.   
      491.         private void button35_Click(object sender, EventArgs e)  
      492.         {  
      493.             DataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.SunkenVertical;  
      494.         }  
      495.   
      496.         private void button36_Click(object sender, EventArgs e)  
      497.         {  
      498.             DataGridView1.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Sunken;  
      499.         }  
      500.   
      501.         private void button37_Click(object sender, EventArgs e)  
      502.         {  
      503.             DataGridView1.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;  
      504.         }  
      505.   
      506.         private void button38_Click(object sender, EventArgs e)  
      507.         {  
      508.             DataGridView1.GridColor = Color.Red;  
      509.         }  
      510.   
      511.         private void button40_Click(object sender, EventArgs e)  
      512.         {  
      513.             //foreach (Control c in this.Controls)  
      514.             //{  
      515.             //    c.Dispose();    //如果有Panel等窗口的话,还得将子控件给 Dispose掉  
      516.             //}  
      517.   
      518.             DataGridView1.Dispose();  
      519.               
      520.             InitializeComponent();  
      521.             dgvLoad();  
      522.         }  
      523.   
      524.         private void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)  
      525.         {  
      526.             DataGridView dgv = (DataGridView)sender;  
      527.             // 如果单元格是“Column1”列的单元格  
      528.             if (e.ColumnIndex < 0)  
      529.             {  
      530.                 return;  
      531.             }  
      532.             if (dgv.Columns[e.ColumnIndex].Name == "Column1" && e.Value is string)  
      533.             {  
      534.                 // 将单元格值改为大写  
      535.                 string str = e.Value.ToString();  
      536.                 e.Value = str.ToUpper();  
      537.                 // 应用该Format,Format完毕。  
      538.                 e.FormattingApplied = true;  
      539.             }  
      540.         }  
      541.   
      542.         private void DataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e)  
      543.         {  
      544.             DataGridView dgv = (DataGridView)sender;  
      545.             //单元格列为“Column2”时  
      546.             if (dgv.Columns[e.ColumnIndex].Name == "Column2" && e.Value is string)  
      547.             {  
      548.                 //将单元格值设为大写  
      549.                 e.Value = e.Value.ToString().ToUpper();  
      550.                 //解析完毕  
      551.                 e.ParsingApplied = true;  
      552.             }  
      553.         }  
      554.   
      555.         private void DataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)  
      556.         {  
      557.             // 设定单元格的默认值  
      558.             e.Row.Cells["Column1"].Value = "我";  
      559.             e.Row.Cells["Column2"].Value = "是";  
      560.             e.Row.Cells["Column3"].Value = "默";  
      561.             e.Row.Cells["Column4"].Value = "认";  
      562.             e.Row.Cells["Column5"].Value = "值";  
      563.         }  
      564.   
      565.         private void DataGridView1_CellErrorTextNeeded(object sender, DataGridViewCellErrorTextNeededEventArgs e)  
      566.         {  
      567.             DataGridView dgv = (DataGridView)sender;  
      568.             // 单元格值为负整数时,Error图标被表示。  
      569.             object cellVal = dgv[e.ColumnIndex, e.RowIndex].Value;  
      570.             if (cellVal is int && ((int)cellVal) < 0)  
      571.             {  
      572.                 e.ErrorText = "不能输入负整数。";  
      573.             }  
      574.         }  
      575.   
      576.         private void DataGridView1_RowErrorTextNeeded(object sender, DataGridViewRowErrorTextNeededEventArgs e)  
      577.         {  
      578.             DataGridView dgv = (DataGridView)sender;  
      579.             if (dgv["Column1", e.RowIndex].Value == DBNull.Value &&  
      580.             dgv["Column2", e.RowIndex].Value == DBNull.Value)  
      581.             {  
      582.                 e.ErrorText =  
      583.                 "Column1和Column2必须输入一个值。";  
      584.             }  
      585.         }  
      586.   
      587.         private void DataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)  
      588.         {  
      589.             DataGridView dgv = (DataGridView)sender;  
      590.   
      591.             if (dgv.Columns[e.ColumnIndex].Name == "Column4" && e.FormattedValue.ToString() == "")  
      592.             {  
      593.                 //行的错误提示的设定  
      594.                 dgv.Rows[e.RowIndex].ErrorText = "值未输入。";  
      595.                 //取消已经输入的内容,还原成上次的输入内容。  
      596.                 //dgv.CancelEdit()  
      597.                 //取消之后的动作  
      598.                 e.Cancel = true;  
      599.             }  
      600.         }  
      601.   
      602.         private void DataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)  
      603.         {  
      604.             DataGridView dgv = (DataGridView)sender;  
      605.             //验证通过的话,则清空行的错误提示  
      606.             dgv.Rows[e.RowIndex].ErrorText = null;  
      607.         }  
      608.   
      609.           
      610.     }  
      611. }  
  • 相关阅读:
    HDU 2878 Great World of Goo
    HDU 4295 4 substrings problem
    UVALive 4975 Casting Spells
    UVA 10572 Black & White
    POJ 3133 Manhattan Wiring
    HDU 4267 A Simple Problem with Integers
    SPOJ 7258 Lexicographical Substring Search
    UVALive 4978 Fields and Farmers
    ssh连接aix问题与解决
    MDRT_<>$表
  • 原文地址:https://www.cnblogs.com/yuer20180726/p/11004764.html
Copyright © 2011-2022 走看看