Navigate Datagrid

G

Gina_Marano

Hey All,

I have the following code example where the user gives me a query and
I populate a grid:

private void btnTestSQL_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();

SqlConnection conn = new SqlConnection(sSrcConnectionStr);
try
{
conn.Open();

SqlDataAdapter da = new SqlDataAdapter(eSqlText.Text,
conn);
da.Fill(ds, "data");
gTest.DataSource = ds;
gTest.DataMember = "data";
ds = null;
da = null;
}
finally
{
conn.Close();
conn.Dispose();
}
}

Works great!

Next I want to navigate through the grid and as I navigate the current
row is highlighted. I know this is inefficient with large datasets but
I want the visual feedback to the user.

I found how to do this using the currencymanager but I also want the
physical grid row to be highlighted as I traverse the grid.

CurrencyManager cm =
(CurrencyManager)this.BindingContext[gTest.DataSource];
int rowCount = cm.Count;

Why does rowCount = 1 when I have 10 rows?

At design time I don't know what column a field value will be in but I
know that the field value must be in the datasource some where. So I
will need to be able to determine that as well.

Thanks much

~Gina_M~
 
N

Nicholas Paldino [.NET/C# MVP]

Gina,

The count should not be 1 if you have multiple rows bound to the grid
(and it's not in a virtual mode).

Can you show a complete example where this occurs? Iterating through
the bindingmanager to iterate through the rows is correct to change the row
and have it reflect in the grid, but finding the column that the cursor is
on will require you to look at the Current (or CurrentCell property, I can't
remember the name offhad) property on the grid to find on which cell the
focus is on.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


You do know that you are wide open to SQL injection right?

What if the user enter : "delete from table" ?
 
G

Gina_Marano

You do know that you are wide open to SQL injection right?

Thanks for the warning but this will be an internal tool used by
development. They have the credentials to drop the entire database if
they want.

I got the bad code example off of one of MS pages...

I found the problem:

CurrencyManager cm =
(CurrencyManager)this.BindingContext[gTest.DataSource];

I needed to include the DataMember as well:

CurrencyManager cm =
(CurrencyManager)this.BindingContext[gTest.DataSource,gTest.DataMember];

BUT I still need to get the field values for the row.

I assume that I can use:

DataGridViewRow row = gTest.CurrentRow;

to get the current row. Since i do not know the order of the field
names, how do I get a field/cell value by name rather than by
position?

Thanks

~Gina_M~
 
G

Gina_Marano

OK, I figured it out...

DataGridViewRow row = gridTest.CurrentRow;
string s = row.Cells["ID"].Value.ToString();

~Gina_M~

You do know that you are wide open to SQL injection right?

Thanks for the warning but this will be an internal tool used by
development. They have the credentials to drop the entire database if
they want.

I got the bad code example off of one of MS pages...

I found the problem:

CurrencyManager cm =
(CurrencyManager)this.BindingContext[gTest.DataSource];

I needed to include the DataMember as well:

CurrencyManager cm =
(CurrencyManager)this.BindingContext[gTest.DataSource,gTest.DataMember];

BUT I still need to get the field values for the row.

I assume that I can use:

DataGridViewRow row = gTest.CurrentRow;

to get the current row. Since i do not know the order of the field
names, how do I get a field/cell value by name rather than by
position?

Thanks

~Gina_M~
 

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