C# Datagridview完整攻略

來源:互聯網
上載者:User

0.行列,寬度自適應,加編號,背景色。

grid.Columns.Add("ID", "ID");             grid.Columns["ID"].DisplayIndex = 0;
            for (int i = 0; i < grid.Rows.Count; i++)
            {
                grid.Rows[i].Cells["ID"].Value = i + 1;
            }
            grid.RowHeadersVisible = false;
            grid.AllowUserToAddRows = false;
            grid.RowsDefaultCellStyle.Font = new Font("宋體", 8, FontStyle.Regular);
            grid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
            grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;


            grid.BackgroundColor = Color.CadetBlue;


1. DataGridView當前的儲存格屬性取得、變更

2. DataGridView編輯屬性

3. DataGridView最下面一列新追加行非表示

4. DataGridView判斷當前選中行是否為新追加的行

5. DataGridView刪除行可否設定

6. DataGridView行列不表示和刪除

1.當前的儲存格屬性取得、變更

[C#]

'當前選中單元的值

Console.WriteLine(DataGridView1.CurrentCell.Value)

'當前列的Index值

 Console.WriteLine(DataGridView1.CurrentCell.ColumnIndex)

'當前單元的行Index值

Console.WriteLine(DataGridView1.CurrentCell.RowIndex)

'將控制項中(0, 0)處的值,賦給目前的儲存格.

DataGridView1.CurrentCell =DataGridView1[0, 0]

2.DataGridView編輯屬性

全部儲存格編輯屬性

[C#]

'DataGridView1隻讀屬性

DataGridView1.ReadOnly = True

指定行列儲存格編輯屬性

[C#]

 

DataGridView1.Columns[1]ReadOnly = True

 

DataGridView1.Rows[2].ReadOnly = True

 

DataGridView1[0, 0].ReadOnly = True

根據條件判斷儲存格的編輯屬性

下例中column2的值是True的時候,Column1設為可編輯

 [C#]

  private   void  DataGridView1_CellBeginEdit( object  sender, DataGridViewCellCancelEventArgs e)
        {
             if  ( this .DataGridView1.Columns[e.ColumnIndex].Name.ToString().Equals( " Column2 " ))
            {
                 if  (DataGridView1[ " Column2 " , e.RowIndex].Value.ToString().ToLower().Equals( " true " ))
                {
                    DataGridView1[ " Column1 " , e.RowIndex].ReadOnly  =   false ;
                }
                 else
                {
                    DataGridView1[ " Column1 " , e.RowIndex].ReadOnly  =   true ;
                }
            }
        }


 

3.DataGridView最下面一列新追加行非表示

[C#]

 

DataGridView1.AllowUserToAddRows = False

4.判斷當前選中行是否為新追加的行

[C#]

if (DataGridView1.CurrentRow.IsNewRow)
{
       Console.WriteLine("當前行,是新添加的行");
}
       else
{
       Console.WriteLine("當前行,不是新添加的行");
}

5. DataGridView刪除行可否設定

[C#]

 

DataGridView1.AllowUserToDeleteRows = False

根據條件判斷當前行是否要刪除

[C#]

 
代碼  1  private   void  DataGridView1_UserDeletingRow( object  sender, DataGridViewRowCancelEventArgs e)
 2          {
 3 
 4 
 5 
 6               if  (MessageBox.Show( " 確定要刪除嗎? " ,  " 刪除確認 " , MessageBoxButtons.OKCancel, MessageBoxIcon.Question).Equals(System.Windows.Forms.DialogResult.OK))
 7              {
 8 
 9              }
10               else
11              {
12                  e.Cancel  =   true ;
13              }
14          }


 

6. DataGridView行列不表示和刪除

行列不表示

[C#]

'DataGridView1的第一列不表示

DataGridView1.Columns[0].Visible = False

'DataGridView1的第一行不表示

DataGridView1.Rows[0].Visible = False

行列表頭部分不表示

[C#]

 

DataGridView1.ColumnHeadersVisible = False

 

DataGridView1.RowHeadersVisible = False

指定行列刪除

[C#]

 

DataGridView1.Columns.Remove("Column1")

 

DataGridView1.Columns.RemoveAt(0)

 

DataGridView1.Rows.RemoveAt(0)

選擇的行列刪除(多行列)

[C#]

'DataGridView1刪除選中的行

foreach (DataGridViewRow r in DataGridView1.SelectedRows)
            {
                if (!r.IsNewRow)
                {
                    DataGridView1.Rows.Remove(r);
                }
            }

7. DataGridView行列寬度高度設定為不能編輯

8. DataGridView行高列幅自動調整

9. DataGridView指定行列凍結

10. DataGridView列順序變更可否設定

11. DataGridView行複數選擇

12. DataGridView選擇的行、列、儲存格取得

 

7. DataGridView行列寬度高度設定為不能編輯

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.