Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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..


C#
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();
       }
Posted
Updated 12-Jan-14 0:57am
v2

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)Add VisibleChanged event handler to your Form like given below
private void Form3_VisibleChanged(object sender, EventArgs e)
       {
           dataGridView1.AllowUserToAddRows = false;
           MakeOnlyCurrentRowVisible();
       }


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.
 
Share this answer
 
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900