2018年9月15日 星期六

[C#] DataGridView

名詞解釋












格子座標











dataGridView1[0, 0].Value = "1";
dataGridView1[1, 0].Value = "2";
dataGridView1[0, 1].Value = "3";
dataGridView1.Rows[0].Cells[0].Value = "1";
dataGridView1.Rows[0].Cells[1].Value = "2";
dataGridView1.Rows[1].Cells[0].Value = "3";

Cell 字型及字體大小
dataGridView1.DefaultCellStyle.Font = New Font("Arial", 9);


列(Row)控制

1.讀取列數

int rowCount;
rowCount = dataGridView1.Rows.Count;

2.新增列 - 空白
dataGridView1.Rows.Add(); //單列
dataGridView1.Rows.Add(2); //兩列

3.新增列 - 含內容







dataGridView1.Rows.Add(new Object[] {“apple”, 200}); //Item, Price

4.隱藏最下列 (列頭為*號,黃色標記處)









dataGridView1.AllowUserToAddRows = false;

5.刪除列
dataGridView1.Rows.RemoveAt(0); //第0列
dataGridView1.Rows.RemoveAt(dataGridView.CurrentRow.Index); //被選則的那一列

6.隱藏列頭,最左行 (黃色標記處)








dataGridView1.RowHeadersVisible = false;

7.每一列頭的名字







dataGridView1.Rows[0].HeaderCell.Value = "Row1";

8.禁止使用者改變DataGridView1的所有列的高
dataGridView1.AllowUserToResizeRows = false;

9.自動滾到最下列,最後新增的列
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.RowCount - 1;


------------------------------------------------------------------------------

行(Colune)控制

1.讀取行數

int columnCount;
columnCount = dataGridView1.Columns.Count;

2.刪除行
DataGridView1.Columns.RemoveAt(0); //第0行
DataGridView1.Columns.Remove("Column1"); //行頭名為Column1的那一行

3.隱藏行頭,最上列 (黃色標記處)








dataGridView1.ColumnHeadersVisible = false;

4.禁止使用者改變所有行寬
dataGridView1.AllowUserToResizeColumns = false;

5.關閉原本點擊 Header 的時候會做排序的動作
dgvRfSetting680FG.Columns[1].SortMode = DataGridViewColumnSortMode.NotSortable; //關閉第1行

6.整行的內容靠右對齊
DataGridView1.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
; //第一行靠右對齊

7.在行加入按鈕










/*Insert button*/
DataGridViewButtonColumn dataGridViewButtonColumn = new DataGridViewButtonColumn();
dataGridViewButtonColumn.Text = "dataGridViewButtonColumn";
dataGridViewButtonColumn.Name = "Button";

/*Insert to first column*/
dataGridView1.Columns.Insert(0, dataGridViewButtonColumn);

/*Named the buttom*/
dataGridView1[0, 0].Value = "Button 1";
dataGridView1[0, 1].Value = "Button 2";
dataGridView1[0, 2].Value = "Button 3";

加入事件(Event),這樣按下按鈕才有動作.








private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    int row, colume;

    /*Read the button position which is pressed*/
    row = e.RowIndex;
    colume = e.ColumnIndex;
}

8.行頭字型及字體大小
dataGridView1.ColumnHeadersDefaultCellStyle.Font = New Font("Arial", 9);

------------------------------------------------------------------------------
格子(Cell)內以十六進位表示
X:十六進位, 2:如果未滿兩位數會補0













結果:
dataGridView1[0, 0].Value = 10;





------------------------------------------------------------------------------

無法至右: SortMode改成NotSortable




















參考資料:
NET 2.0 - WinForm Control - DataGridView 編程36計(一)
https://blog.csdn.net/fangxing80/article/details/1561011#A6
.NET 2.0 - WinForm Control - DataGridView 编程36计(二)
https://blog.csdn.net/fangxing80/article/details/1821425




沒有留言:

張貼留言