.Net 2.0 Gridview +check if column exists

Soldato
Joined
7 Jun 2005
Posts
3,035
Location
SE London
Hi all,

basically i want to check if a certain defined gridview has a column with a particular name.

I have the column name as a string value somewhere.

I wanted to do something like this:

if (myGridView.Columns.Contains(myColumnName))

however, the Contains method expects a datacolumnField object.

Is there another way of doing this?

thanks!
 
Use the find method and a predicate that searches using the name field, gimme a sec for example code

Scratch that is the list doesn't have a find method, however their is an overloaded .Contains method that accepts columnname as a parameter so you can use your variable that holds the column name
 
Last edited:
cant you do something like this...


Code:
        Dim objColumn As DataGridViewColumn
        For Each objColumn In myGridView.Columns
            If objColumn.Name = "xxxxx" Then

            End If
        Next
 
eriedor said:
however their is an overloaded .Contains method that accepts columnname as a parameter so you can use your variable that holds the column name

how do I access this method? I can only see 1 ".Contains" method for myGridview.Columns and I'm using .Net 2.0

Numpty - yes that is a solution, however i'm looking for a neater and faster one.
 
NumptyUK said:
try

If myGridView.Columns.Contains(strFieldName) = True Then

instead then

Yes that would have been the ideal solution, however the Contains method doesn't take a string, it takes a DataControlField and this doesn't work as you'll be comparing the whole object, not just the column name.
 
well im on .Net 2 as well, with vb.net and that is defenitly an overloaded method of the datagridview. You must have a different control. Sorry.
 
NumptyUK said:
well im on .Net 2 as well, with vb.net and that is defenitly an overloaded method of the datagridview. You must have a different control. Sorry.

Hmm. Surely the methods offered by the .Net framework 2.0 should be equal and independant of programming language?

I'm using a Gridview control and looking at the Columns collection property.
 
could you not just use an oledbcommand and connection to the data with a variable column field in the command to check the datasource for a particular column. Then handle the error if no field is found, no error means it was found.
 
Ladforce said:
could you not just use an oledbcommand and connection to the data with a variable column field in the command to check the datasource for a particular column. Then handle the error if no field is found, no error means it was found.

Ehm that's even more long-winded that looping through all columns in the gridview. At least then you don't have to access any database....
 
Ladforce said:
it would be quicker than cycling through each column though. especially if it was a large datasource.

Ehm the Gridview and columns are already present in memory, the database table structure needs to be loaded, so i doubt that will be quicker.
 
ahhh got it. Youre using the GridView , we're talking about the DataGridView. Two different controls, yours is a cut down version for webforms? At least thats what the references makes it look like in Studio anyway. So if your doing a desktop app, use the DataGridView, if your writing a web page, youre stuffed and have to do it the "slow" way, im sure the users wont mind the few extra milliseconds it will take to check the column names :)

it wont matter how big the datasource is as its just checking the column headers or the column object so dont get too worried about the speed.
 
Last edited:
NumptyUK said:
ahhh got it. Youre using the GridView , we're talking about the DataGridView. Two different controls, yours is a cut down version for webforms? At least thats what the references makes it look like in Studio anyway. So if your doing a desktop app, use the DataGridView, if your writing a web page, youre stuffed and have to do it the "slow" way, im sure the users wont mind the few extra milliseconds it will take to check the column names :)

Darn - i'll just iterate through the columns then and find a match - it's like re-writing that "Contains" method. Perhaps I should extend the GridView object and add my method ?
 
well...what i mean is, you dont even need a datagridview to test the data if you use a connection/command to check it. Surely the time taken to populate the datagrid and cycle through it would be longer as the data has to be loaded from somewhere?
 
Ladforce said:
well...what i mean is, you dont even need a datagridview to test the data if you use a connection/command to check it. Surely the time taken to populate the datagrid and cycle through it would be longer as the data has to be loaded from somewhere?

I'm loading the data through a datasource into a Gridview. Then afterwards, i want to sort on a particular field, but only if that column exists in the gridview. So checking if the column exists in the Gridview is the fastest and easiest way to do this.

I agree - you don't need a gridview to check if a particular column exists in a database table.
 
Back
Top Bottom