to: wxfwxfwxfwxf(wxfwxfwxfwxf)、 mswater(起点低/底子薄) 两位提供的方法都忽略了一个问题:排序。 当用户在DataGrid上单击某一列的列头后,DataGrid会按照单击的列进行排序,而DataSet的排序还是没变,这样CurrentRowIndex、CurrentCell.RowNumber的序号都和DataSet中的对不上了
DataGrid是可以多选的,你只能通过遍历来取得,例如: string s=""; for (int i=0;i<this.dataGrid1.BindingContext[this.dataGrid1.DataSource].Count;i++) { if (this.dataGrid1.IsSelected(i)) { s+=((DataView)((CurrencyManager)this.dataGrid1.BindingContext[this.dataGrid1.DataSource]).List)[i][0].ToString()+Environment.NewLine; } } MessageBox.Show("The lines you selected are :"+s);如果你想得到左边的箭头指向的项,你可以用下面的办法 DataGrid dataGrid1; ... DataRow dr=((DataRowView)this.dataGrid.BindingContext[this.dataGrid1.DataSource].Current).Row;
DataRow dr = this.ds.Tables[0].DefaultView[ this.dataGrid.CurrentRowIndex].Row;我用过,没有任何问题!1
dataGrid1[dataGrid1.CurrentRowIndex,0].ToString().Trim();
依次类推。
两位提供的方法都忽略了一个问题:排序。
当用户在DataGrid上单击某一列的列头后,DataGrid会按照单击的列进行排序,而DataSet的排序还是没变,这样CurrentRowIndex、CurrentCell.RowNumber的序号都和DataSet中的对不上了
string s="";
for (int i=0;i<this.dataGrid1.BindingContext[this.dataGrid1.DataSource].Count;i++)
{
if (this.dataGrid1.IsSelected(i))
{
s+=((DataView)((CurrencyManager)this.dataGrid1.BindingContext[this.dataGrid1.DataSource]).List)[i][0].ToString()+Environment.NewLine;
}
}
MessageBox.Show("The lines you selected are :"+s);如果你想得到左边的箭头指向的项,你可以用下面的办法
DataGrid dataGrid1;
...
DataRow dr=((DataRowView)this.dataGrid.BindingContext[this.dataGrid1.DataSource].Current).Row;