DataGridView

G

Grant Schenck

Hello,

OK, my question is pretty basic. Specifically, how do I change a column of
a DataGridView to a combobox at runtime?

I've building a fairly simple application to support making changes to a
database.

The DB has a number of tables, some of which are related.

I create data adapters for several of the related tables using
SQLDataAdapter:
//-----------------------------------------
string strQuery = "Select * from RecordTrigger";
m_daRecordTrigger = new SqlDataAdapter(strQuery, con);
SqlCommandBuilder builder = new
SqlCommandBuilder(m_daRecordTrigger);

//-----------------------------------------
strQuery = "Select * from StationList";
m_daStationList = new SqlDataAdapter(strQuery, con);
builder = new SqlCommandBuilder(m_daStationList);

I create a dataset and fill the dataset with my two tables:
//-----------------------------------------
m_ds = new DataSet("STPSRecorder");

//-----------------------------------------
m_daRecordTrigger.Fill(m_ds, "RecordTrigger");
m_daStationList.Fill(m_ds, "StationList");

I create a relation between the two tables based on the StationListID column
common to both tables:
m_ds.Relations.Add(
"StationListRecordTrigger",
m_ds.Tables["StationList"].Columns["StationListID"],
m_ds.Tables["RecordTrigger"].Columns["StationListID"]);

Finally, I set the datasource of a dataviewgrid that I added to my form:
dataGridView1.DataSource = dtRecordTrigger;

This works fine, the RecordTrigger table shows in the dataviewgrid and each
row shows the contents of the corresponding row in the database.

The problem I have is that I want to change the contol for column which
shows the StationListID from a text field to combobox and I want the combo
box to reflect the names which correspond to the various IDs in the
StationList table.

I;'ve been able to get what I wnat if I use the design time Wizards and
properties. Specifically I can drill down to my column in the view and
change the type to a combo box and setting the appropriate properties I get
a list of the StationList names. However, what I can figure out is how to
change the column at runtime. What is the trick?

Thanks!
 
G

Gregory A. Beamer

I;'ve been able to get what I wnat if I use the design time Wizards
and properties. Specifically I can drill down to my column in the
view and change the type to a combo box and setting the appropriate
properties I get a list of the StationList names. However, what I can
figure out is how to change the column at runtime. What is the trick?

You have to remove the control already in the cell and replace with a
combobox. This can be done in the row binding event handler. I am
assuming ASP.NET here, but the method for windows forms is similar.

Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
G

Grant Schenck

Actually Win Forms...
--
Grant Schenck


Gregory A. Beamer said:
You have to remove the control already in the cell and replace with a
combobox. This can be done in the row binding event handler. I am
assuming ASP.NET here, but the method for windows forms is similar.

Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 

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