1、 DataGridView 控件詳細解說:http://blog.csdn.net/qq_24304137/article/details/51068768
2、
DataGridView的幾個基本操作:
1、獲得某個(指定的)單元格的值:
dataGridView1.Row[i].Cells[j].Value;
2、獲得選中的總行數:
dataGridView1.SelectedRows.Count;
3、獲得當前選中行的索引:
dataGridView1.CurrentRow.Index;
4、獲得當前選中單元格的值:
dataGridView1.CurrentCell.Value;
5、取選中行的數據
string[] str = new string[dataGridView.Rows.Count];
for(int i;i<dataGridView1.Rows.Count;i++)
{
if(dataGridView1.Rows[i].Selected == true)
{
str[i] = dataGridView1.Rows[i].Cells[1].Value.ToString();
}
}
7、獲取選中行的某個數據
int a = dataGridView1.SelectedRows.Index;
dataGridView1.Rows[a].Cells["你想要的某一列的索引,想要幾就寫幾"].Value;
獲得某個(指定的)單元格的值: dataGridView1.Row[i].Cells[j].Value; Row[i] 應該是Rows[i]
int a=dataGridView1.CurrentRow.Index;
string str=dataGridView1.Row[a].Cells["strName"].Value.Tostring();
selectedRows[0]當前選中的行
.cell[列索引].values 就是當前選中行的某個單元格的值
DataGridView1.SelectedCells(0).Value.ToString 取當前選擇單元內容
DataGridView1.Rows(e.RowIndex).Cells(2).Value.ToString 當前選擇單元第N列內容
3、datagridview獲取某個單元格的值
首先是列定位
dataGridView1.Rows[i].Cells[*].value.tostring();
*這里的參數可以是int index,也可以是string columnName.
column除了name,index以外還有個text,也就是列頭文本.name和index可以直接定位,text不行.
想要用text定位就得建一個方法,循環dataGridView.columns,通過匹配text得到name或者index屬性.(用這個方法還得保證列標題文本在表格內唯一,否則不一定能定位到想要的列)
4、datagridview中最后一行某列的數據
DataGridView1.Rows[DataGridView1.RowCount-1].Cells[列號].Value
【歡迎轉載】
轉載請表明出處: 樂學習