Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGridView getting bound column name. 1

Status
Not open for further replies.

Kliot

Programmer
Jan 10, 2003
622
US
Hi,

I have a DataGridView and I want to get the data colunmn name for a particular column. What I want is not the DataGridView column name but the coulun name of the bound database.

I can do this with a textbox by using
tbx.DataBindings.Item(0).BindingMemberInfo.BindingField()

I just can't figure out how to get this information from a DataGridView.

Thanks
Perrin
 
Is the DataGridView bound to a DataTable? The DataTable has a Columns() collection as a property.
 
Yes the DataGridView is bound to a sql server datatable. How do I use the columns() collection to get the datatable column name?

 
You iterate through it like any other collection. Try this code.

Code:
        For Each dc As DataColumn In YourDataTable.Columns
            MessageBox.Show(dc.ColumnName)
        Next
 
RiverGuy

This gives me the columnName of the DataGridView, this is not what I want. I want the column name of the bound data table column, this is not necessarly the same as the DataGridView column name. I need to get this from the databindings and this is what I'm not shure how to do.

Thanks
 
That's incorrect. In my code, replace the word YourDataTable with the name of your DataTable. Or, you can use something like YourDataSet.Tables("TheNameOfYourTable") This has nothing to do with the DataGridView.
 
RiverGuy,

We are getting a little off base here, I need to work with the datagridview because I need to know the name of the datatable column that is bound to the currently selected column in the DataGridView.

 
TheNameOfYourDataGridView.Columns(TheNumericalIndexOfTheColumnYouWantToCheck).DataPropertyName
 
RiverGuy,

You da man as usual. It seems so obvious now, but then that's the story of my life, thanks.

Perrin
 
The same could be said for me. I've been known to misunderstand a question every now and then. [thumbsup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top