使用bindingsource在datagridview中只显示一行 [英] Show only one row in datagridview using bindingsource
问题描述
您好我是c#的新手,我正在使用winform。我想在datagridview中只显示一行。
当我导航到bindingsource中的下一个或上一个记录时,我只想显示来自datagridview中bindingsource的
当前行。我尝试过滤,但它显示了1个记录而不是1个< allrecords>中的
1。这是我的代码,请建议..
Hi I am new to c# and i am working on winform .I want to show only one row in datagridview.
when I navigate to next or previous record in bindingsource ,I want to show only the
current row from bindingsource in datagridview .I have tried with filtering, but it shows
1 of 1 records instead of 1 of <allrecords>. here is my code , please suggest..
private void Form3_Load(object sender, EventArgs e)
{
string cSql = "SELECT * FROM RFID_MAS";
dadapter = new SqlDataAdapter(cSql, Conn);
dadapter.Fill(dset);
bindingNavigator1.BindingSource = bs;
bs.DataSource = dset.Tables[0];
dataGridView1.DataSource = bs;
bs.PositionChanged += bs_PositionChanged;
// dset.Tables.Add();
//dset.Tables[1].
}
void bs_PositionChanged(object sender, EventArgs e)
{
bs.Filter = "SEQ_NO =" + ((DataRowView)bs.Current).Row["SEQ_NO"].ToString();
//dset.Tables[0].DefaultView.RowFilter = "SEQ_NO ="+ ((DataRowView)bs.Current).Row["SEQ_NO"].ToString();
}
推荐答案
I通过以下步骤想出了一种做法:
1)在你的代码中添加下面提到的方法
I figured out one way of doing it by following below steps
1) Add below mentioned method to your code
private void MakeOnlyCurrentRowVisible()
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.CurrentRow != dataGridView1.Rows[i])
dataGridView1.Rows[i].Visible = false;
}
}
2)将VisibleChanged事件处理程序添加到您的表单中,如下所示
2)Add VisibleChanged event handler to your Form like given below
private void Form3_VisibleChanged(object sender, EventArgs e)
{
dataGridView1.AllowUserToAddRows = false;
MakeOnlyCurrentRowVisible();
}
3)将代码添加到dataGridView1_SelectionChanged事件,如下所示
3) Add code to dataGridView1_SelectionChanged event like below
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (!dataGridView1.AllowUserToAddRows)
{
MakeOnlyCurrentRowVisible();
}
}
希望它会有所帮助。
Hope it will help.
private void Form3_Load(object sender,EventArgs e)
{
string cSql =SELECT top(1)FROM RFID_MAS;
dadapter = new SqlDataAdapter(cSql,Conn);
dadapter.Fill(dset);
bindingNavigator1.BindingSource = bs;
bs。 DataSource = dset.Tables [0];
dataGridView1.DataSource = bs;
bs.PositionChanged + = bs_PositionChanged;
// dset。 Tables.Add();
//dset.Tables [1]。
}
试试这个
private void Form3_Load(object sender, EventArgs e)
{
string cSql = "SELECT top(1) FROM RFID_MAS";
dadapter = new SqlDataAdapter(cSql, Conn);
dadapter.Fill(dset);
bindingNavigator1.BindingSource = bs;
bs.DataSource = dset.Tables[0];
dataGridView1.DataSource = bs;
bs.PositionChanged += bs_PositionChanged;
// dset.Tables.Add();
//dset.Tables[1].
}
Try this
这篇关于使用bindingsource在datagridview中只显示一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!