//dv1 是DataGridView控件
public partial class Form1 : Form
{
DataSet ds;
SqlConnection conn;
BindingManagerBase bm;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
conn = new SqlConnection("server=(local);database=HR;user=sa;pwd=");
conn.Open();
SqlDataAdapter da=new SqlDataAdapter("select id,empid,empname from basemployee", conn);
ds = new DataSet();
da.Fill(ds);
dv1.DataSource = ds.Tables[0];
dv1.Refresh(); bm = this.BindingContext[ds.Tables[0]];
} private void button2_Click(object sender, EventArgs e)
{
DataView dw = ds.Tables[0].DefaultView;
if (dw.RowFilter == "Empname Like '%刘%'")
{
dw.RowFilter = "";
}
else
{
dw.RowFilter = "Empname Like '%刘%'";
}
}
private void button1_Click(object sender, EventArgs e)
{
ds.Tables[0].Rows[bm.Posistion][1]="刘德华";
}
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show(bm.Position.ToString());
}
}DataTable中有数据行DataRow,而在DataGridView中没有行这个对象,这让人感到很不习惯,也觉得不够自然。在DataTable中,一张表的层次结构很清楚,DataTable.Rows属性可以得到这张表所包含的所有行的行集,通过行集的索引DataRowCollection[index]就可以得到具体的一个DataRow,数据行的索引DataRow[index]又可以得到这一行的具体某一列的内容。
而DataGridView中显示的数据可能经过DataView的DataView.RowFilter属性、DataView.RowStateFilter属性的过滤,还可能经过DataGridView本身根据各个列的正向和反向排序,所以DataGridView的CurrentRow.Index属性所指示的行索引跟其对应的DataTable的行索引有很大的机会是不一样的,不能够根据DataGridView的CurrentRow.Index去获取其对应的DataTable的行。问题是: 我可以通过 DataRowView ddr = (DataRowView)bm.Current; ddr.Row 来获取当前选中的行.这不是问题.
出于一些原因,我想通过 DataSet.Tables[行号][列号] 的方式选到当前行,可如上面所说,无论是CurrentRowIndex还是Position都受RowFilter的影响,得到的是在DataGridView中的行号,而不是Table中的行号.
各位高手有什么办法得到DataGridView中选中的行在Table中对应的行号么?
public partial class Form1 : Form
{
DataSet ds;
SqlConnection conn;
BindingManagerBase bm;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
conn = new SqlConnection("server=(local);database=HR;user=sa;pwd=");
conn.Open();
SqlDataAdapter da=new SqlDataAdapter("select id,empid,empname from basemployee", conn);
ds = new DataSet();
da.Fill(ds);
dv1.DataSource = ds.Tables[0];
dv1.Refresh(); bm = this.BindingContext[ds.Tables[0]];
} private void button2_Click(object sender, EventArgs e)
{
DataView dw = ds.Tables[0].DefaultView;
if (dw.RowFilter == "Empname Like '%刘%'")
{
dw.RowFilter = "";
}
else
{
dw.RowFilter = "Empname Like '%刘%'";
}
}
private void button1_Click(object sender, EventArgs e)
{
ds.Tables[0].Rows[bm.Posistion][1]="刘德华";
}
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show(bm.Position.ToString());
}
}DataTable中有数据行DataRow,而在DataGridView中没有行这个对象,这让人感到很不习惯,也觉得不够自然。在DataTable中,一张表的层次结构很清楚,DataTable.Rows属性可以得到这张表所包含的所有行的行集,通过行集的索引DataRowCollection[index]就可以得到具体的一个DataRow,数据行的索引DataRow[index]又可以得到这一行的具体某一列的内容。
而DataGridView中显示的数据可能经过DataView的DataView.RowFilter属性、DataView.RowStateFilter属性的过滤,还可能经过DataGridView本身根据各个列的正向和反向排序,所以DataGridView的CurrentRow.Index属性所指示的行索引跟其对应的DataTable的行索引有很大的机会是不一样的,不能够根据DataGridView的CurrentRow.Index去获取其对应的DataTable的行。问题是: 我可以通过 DataRowView ddr = (DataRowView)bm.Current; ddr.Row 来获取当前选中的行.这不是问题.
出于一些原因,我想通过 DataSet.Tables[行号][列号] 的方式选到当前行,可如上面所说,无论是CurrentRowIndex还是Position都受RowFilter的影响,得到的是在DataGridView中的行号,而不是Table中的行号.
各位高手有什么办法得到DataGridView中选中的行在Table中对应的行号么?
DataRow[] foundRows = ds.Tables[0].Select(expression);
foundRows[0]里的就是你要的
例如DataSet.Tabls[0]有三条记录
ID Name
a 张学友
b 刘德华
c 刘邦
显示在DataGridView中也是三条记录.你选中第一行ID=a.那么 DataGridView的CurrentRow.Index 属性就是0.
DataSet.Tables[0].Rows[0]就是你要选中的那行. 相应的,我可以用DataSet.Tables[0].Rows[CurrentRow.Index]
来直接选取在DataSet.Tables[0]中的这一行.
但是DataGridView中显示的数据可能经过DataView的DataView.RowFilter属性、DataView.RowStateFilter属性的过滤,还可能经过DataGridView本身根据各个列的正向和反向排序,所以DataGridView的CurrentRow.Index属性所指示的行索引跟其对应的DataTable的行索引有很大的机会是不一样的,不能够根据DataGridView的CurrentRow.Index去获取其对应的DataTable的行。
例如上面的数据. DataSet.Tables[0].DefautlView.RowFilter="Name Like '刘%'";
那么DataGridView中只显示两行了
ID Name
b 刘德华
c 刘邦
但是DataSet.Tables[0]中的记录位置是不变的.所以你在DataGridView中选中第一行,也就是ID="b"的那行时
DataGridView的CurrentRow.Index 属性是0,但是ID="b"的记录在DataSet.Tables[0]中的行号是1. 这个时候,
DataSet.Tables[0].Rows[CurrentRow.Index]已经不能正确选取到正确的记录了.所以,我想知道有什么办法得到 DataGridView中选中的行在Table中绝对位置对应的行号么.也就是那个DataSet.Tables[0].Rows[i]中的那个i 啊.这下子,有表达清楚了吧?汗.................
------------------------------------------------------------------
如果选择了第一行,我觉得应该用e.RowIndex,如:
DataGridView.Rows[e.RowIndex].Cells[第N列].Value.ToString()
再用ds.Tables[0].Select方法得到相应行号
DataRowView ddr = (DataRowView)bm.Current;ddr.Row 不是更好么?因为DataTable中的记录位置是不变的,而DataGridView中行的位置是可以变化的,不能简单的通过RowIndex来映射.
我是想知道,有没有什么好的方法,能求出DataGridView中选中的行,在DataTable中对应的位置是什么.DataSet.Tables[0].Rows[i].............只是这个i...
DataSet.Tables[0].Rows[i].............只是这个i...
DataRowView ddr = (DataRowView)bm.Current;ddr.Row 不是更好么?
------------------------------------------------------------------老兄,你不是要ds.Tables[0]中的行号吗?Select后的行记录不就包含行号吗,怎么能跟DataRowView ddr = (DataRowView)bm.Current;比
//dv1 是DataGridView控件
public partial class Form1 : Form
{
DataSet ds;
SqlConnection conn;
BindingManagerBase bm;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
conn = new SqlConnection("server=(local);database=HR;user=sa;pwd=");
conn.Open();
SqlDataAdapter da=new SqlDataAdapter("select id,empid,empname from basemployee", conn);
ds = new DataSet();
da.Fill(ds);
dv1.DataSource = ds.Tables[0];
dv1.Refresh(); bm = this.BindingContext[ds.Tables[0]];
bm.PositionChanged += new EventHandler(button3_Click);
} private void button2_Click(object sender, EventArgs e)
{
DataView dw = ds.Tables[0].DefaultView;
if (dw.RowFilter == "Empname Like '%刘% '")
{
dw.RowFilter = "";
}
else
{
dw.RowFilter = "Empname Like '%刘% '";
}
}
private void button1_Click(object sender, EventArgs e)
{
ds.Tables[0].Rows[bm.Posistion][1]="刘德华";
}
private void button3_Click(object sender, EventArgs e)
{
bm.Position=Int32.Parse( bindingNavigator1.PositionItem.ToString());
// MessageBox.Show(bindingNavigator1.PositionItem.ToString())
}
}
用得到的行号找到DataGridView的某个字段ID
再通过ID 找到 Dataset 的位置..
比较笨
前面已经解释了很多了
((DataRowView)DataDridView.SelectedRows[0].DataBoundItem).Row通过DataTalbe 中指定的行,来选中DataGridView 中的行
......
我现在也想这样做,但是没有好的办法,循环肯定不是好办法。楼主如果解决了,麻烦分享一下。
DataRow[] rows = dt.Tables[0].Select(dw.RowFilter,dw.Sort);
rows[bm.Posistion][1]就是符合你要求的东西。
DataView dv = new DataView( yourDataTable, currentSortString, null, DataViewRowState.CurrentRows );
int nRowIndex = dv.Find( yourIDValue );
yourGridView.Select( nRowIndex );
[VB.net]Dim sRow as DataRow=yourDataTable.DefaultView(yourDataGridView.CurrentRow.Index).Row