Very Basic DataGrid Help Needed

  • Thread starter Thread starter jm
  • Start date Start date
J

jm

I manually put a datagrid in on a form. I then added this code to an
event to populate it:

OdbcConnection conn = new OdbcConnection("DSN=CardBase");
sqlCardHolders = "SELECT * FROM tblcardholders " +
"WHERE last_name LIKE '%" + txtLastName.Text + "%'";
DataSet rs = new DataSet();
conn.Open();
OdbcDataAdapter myOdbcAdapter = new
OdbcDataAdapter(sqlCardHolders, conn);
myOdbcAdapter.Fill(rs);
dgridSearchResults.DataSource = rs;
dgridSearchResults.SetDataBinding(rs,"CardHolders");

I do not see anything in the datagrid except a little box with a plus
and I can eventually expand it to show the row and it lets me edit it,
etc.

If I turn off Row Headers Visible, I don't even see the little plus.

I just want to query the database and have the rows show up. I don't
want to edit them or anything. I just can't find anything that shows
me some basic tutorial on this. Thank you for any help.
 
jm,

The DataGrid is doing that because you are assigning it a DataSet as a
DataSource. A DataSet contains a collection of Tables and possibly
Relationships between those tables. If you want to only represent the
DataTable in the collection represented by your single query, you'll need to
specify the DataSource as being the DefaultView of the table you want to
show.

For example:
dgridSearchResults.DataSource = rs.Tables[0].DefaultView;

Hope that helps,

Patrick Altman
 

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

Back
Top