Find DataGrid Column No

G

Guest

How do I find the column (index) number at runtime of a particular DataGrid column if I know the column header string

//somthing like the following
int x = datagrid.column["ID"];
 
S

Shakir Hussain

Try this

string myText = "My Column Text";

int position= ((DataTable)this.dataGrid1.DataSource).Columns[myText].Ordinal

--
Shak
(Houston)



Steve B. said:
How do I find the column (index) number at runtime of a particular
DataGrid column if I know the column header string
//somthing like the following
int x = datagrid.column["ID"];
 
G

Guest

Thanks for your input. A runtime error says "Specified [DataTable] cast is invalid".

object a = dataGrid.DataSource; // returns a DataView object

I'm still working on it. Note: I also know the table Name as well as the column

Shakir Hussain said:
Try this

string myText = "My Column Text";

int position= ((DataTable)this.dataGrid1.DataSource).Columns[myText].Ordinal

--
Shak
(Houston)



Steve B. said:
How do I find the column (index) number at runtime of a particular
DataGrid column if I know the column header string
//somthing like the following
int x = datagrid.column["ID"];
 
G

Guest

Thanks for your input. A runtime error states a "Specified cast [DataTable] is invalid"

object a = dGrid.DataSource; //returns DataView obj

I also know the table name as well as the column name

Steve

Shakir Hussain said:
Try this

string myText = "My Column Text";

int position= ((DataTable)this.dataGrid1.DataSource).Columns[myText].Ordinal

--
Shak
(Houston)



Steve B. said:
How do I find the column (index) number at runtime of a particular
DataGrid column if I know the column header string
//somthing like the following
int x = datagrid.column["ID"];
 
G

Guest

This procedure seems to find the column number of the DG...so far.

public int getDataGridColumnNumber(DataGridTableStyle dataGridStyle, string columnName)
{
int columnNumber = -1;

foreach(DataGridColumnStyle colStyle in dataGridStyle.GridColumnStyles)
{
columnNumber = columnNumber + 1;
if(colStyle.HeaderText.CompareTo(columnName) == 0)
return columnNumber;
}
columnNumber = -1;
return columnNumber;
}

Steve B. said:
I solved the casting error by binding dGrid.DataSource = dataTable;
However, running the previous statement gets the column location in the dataset NOT the datagrid.

This is a show stopper because I move columns around and must bindContents()
Ideas. Why is this difficult?


Steve B. said:
Thanks for your input. A runtime error states a "Specified cast [DataTable] is invalid"

object a = dGrid.DataSource; //returns DataView obj

I also know the table name as well as the column name

Steve

Shakir Hussain said:
Try this

string myText = "My Column Text";

int position= ((DataTable)this.dataGrid1.DataSource).Columns[myText].Ordinal

--
Shak
(Houston)



How do I find the column (index) number at runtime of a particular
DataGrid column if I know the column header string

//somthing like the following
int x = datagrid.column["ID"];
 

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