How programaticly change row in a GridView?

  • Thread starter Martin Arvidsson
  • Start date
M

Martin Arvidsson

Hi!

I have used the SqlCommand and DataReader to read data to a DataSet
This dataset is the used to set the properties; DataMember and DataSource of
the DataGridView.

Now what i want to do is to go to a specific row of the DataGridView.
I have tried to use a BindingSource to find the value and the issue a
position, but it doesn't work

What i want to do is when i acces the grid wich is called as a prompt i want
to position my self on the value based on the field that i call the lookup
from....

Regards
Martin
 
C

Cor Ligthert [MVP]

Martin,

You are making a message that is very confusing,
Are you writing in your subject about a GridView and therefore a
WebApplication and therefore using a datareader, or are you talking as in
your text about a DataGridView and therefore a windowforms application and
confuse us even more that you are not using a dataadapter or tableadapter to
get the dataset.

(By the way, in a webapplication it would in my idea as well more efficient
to use the two last ones, to be sure your dataset is correct and the
positioning using the bindingsource goes well).

So maybe can you help us to come out of this confusing things?

Cor
 
M

Martin Arvidsson

Hi!

Sorry it should be DataGridView...

Here is a code snippet how it looks today
I use this form to select a value from a table. If a value is already in the
calling TextBox, i want to
place the "cursor" on the row in the lookup form
/Martin

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ControlDBLookup
{
public partial class Lookup : Form
{
private string TableSchema;
private string TableName;
private SqlConnection conn;
private string[] DisplayColumns;
private DataSet LookupData;
private DBLookup theLookup;
public string Lookup_TableSchema
{
set { TableSchema = value; }
}
public string Lookup_TableName
{
set { TableName = value; }
}
public SqlConnection Lookup_SqlConnection
{
set { conn = value; }
}
public string[] Lookup_DisplayColumns
{
set { DisplayColumns = value; }
}
public DataSet Lookup_LookupData
{
get { return LookupData; }
}
public Lookup(DBLookup ALookup)
{
InitializeComponent();
theLookup = ALookup;
}
private void Lookup_Load(object sender, EventArgs e)
{
if (conn != null)
{
DBHelper db = new DBHelper(conn);
LookupData = db.GetLookupData(TableSchema, TableName, DisplayColumns);
if (LookupData != null)
{
gvLookupData.DataSource = LookupData;
gvLookupData.DataMember = LookupData.Tables[0].TableName;
}
}
}
private void btnOK_Click(object sender, EventArgs e)
{
theLookup.ValueData = gvLookupData.CurrentRow.Cells[0].Value;
this.Close();
}

}
}
 
G

Guest

I have the same question. I have a windows form with a DataGridView, which I
want to programatic to set the row.

Martin Arvidsson said:
Hi!

Sorry it should be DataGridView...

Here is a code snippet how it looks today
I use this form to select a value from a table. If a value is already in the
calling TextBox, i want to
place the "cursor" on the row in the lookup form
/Martin

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ControlDBLookup
{
public partial class Lookup : Form
{
private string TableSchema;
private string TableName;
private SqlConnection conn;
private string[] DisplayColumns;
private DataSet LookupData;
private DBLookup theLookup;
public string Lookup_TableSchema
{
set { TableSchema = value; }
}
public string Lookup_TableName
{
set { TableName = value; }
}
public SqlConnection Lookup_SqlConnection
{
set { conn = value; }
}
public string[] Lookup_DisplayColumns
{
set { DisplayColumns = value; }
}
public DataSet Lookup_LookupData
{
get { return LookupData; }
}
public Lookup(DBLookup ALookup)
{
InitializeComponent();
theLookup = ALookup;
}
private void Lookup_Load(object sender, EventArgs e)
{
if (conn != null)
{
DBHelper db = new DBHelper(conn);
LookupData = db.GetLookupData(TableSchema, TableName, DisplayColumns);
if (LookupData != null)
{
gvLookupData.DataSource = LookupData;
gvLookupData.DataMember = LookupData.Tables[0].TableName;
}
}
}
private void btnOK_Click(object sender, EventArgs e)
{
theLookup.ValueData = gvLookupData.CurrentRow.Cells[0].Value;
this.Close();
}

}
}

Cor Ligthert said:
Martin,

You are making a message that is very confusing,
Are you writing in your subject about a GridView and therefore a
WebApplication and therefore using a datareader, or are you talking as in
your text about a DataGridView and therefore a windowforms application and
confuse us even more that you are not using a dataadapter or tableadapter
to get the dataset.

(By the way, in a webapplication it would in my idea as well more
efficient to use the two last ones, to be sure your dataset is correct and
the positioning using the bindingsource goes well).

So maybe can you help us to come out of this confusing things?

Cor
 
Q

Quoc Linh

The DataGridItem object (which is a row on the grid) has the
DataSetIndex property. If you have the dataset that binds to the grid
at the time, use this index to access the row

If not, to get to the row, the more efficient way is use the Select
method on the Table object, not using the view.

To do this,
Define primary key on your table object.
Save the primary key value some where in your app
When need the row, do query on the table based on your primary key

some sample code:

//Define primary key on table
MyTable.PrimaryKey = new DataColumn[] {MyTable.Columns["ID"]};

//Access the row
DataRow[] rows = MyView.Table.Select("ID = " + rowID.ToString());

HTH,
Quoc Linh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top