DataTable Column Index for Given ColumnName

G

Guest

I have a datatable that is created dynamically (in an asp.net 2003
application), so various columns may or may not be present. Consequently,
although the column names may be constant, the column indexes are not. I
would like to retrieve the column index for a given column name, but haven't
been able to find the answer (i'm sure the real reason I can't find the
answer is because I haven't asked the right question). Any ideas?

thanks
 
G

Guest

Hi there Pete,

First, post all strictly ASP.NET questions to ASP.NET newsgroup. Second,
what you're doing is OK but if criteria are known during data binding process
you can also handle itemdatabound event:

protected void rptTwoSpecials_ItemDataBound(object sender,
RepeaterItemEventArgs e)
{
RepeaterItem item = e.Item;

if (item.ItemType == ListItemType.Item ||
item.ItemType == ListItemType.AlternatingItem)
{
Control panel = item.FindControl("co2");
if (panel != null)
panel.Visible = criteriaAreMet;
}
}
 
G

Guest

There is a method of DataColumnCollection called IndexOf(string columnName) or
IndexOf(DataColumn). Both ADO 1.1 and ADO 2.0 support it:

int index = myDataTable.Columns.IndexOf("searchedColumnName");
if (index < 0)
{
// not found
}
else
{
DataColumn dc = myDataTable.Columns[index];
// do whatever
}

Hope this helps
 
G

Guest

thanks - that does the trick.
--
dchman


Milosz Skalecki said:
There is a method of DataColumnCollection called IndexOf(string columnName) or
IndexOf(DataColumn). Both ADO 1.1 and ADO 2.0 support it:

int index = myDataTable.Columns.IndexOf("searchedColumnName");
if (index < 0)
{
// not found
}
else
{
DataColumn dc = myDataTable.Columns[index];
// do whatever
}

Hope this helps
--
Milosz


dchman said:
I have a datatable that is created dynamically (in an asp.net 2003
application), so various columns may or may not be present. Consequently,
although the column names may be constant, the column indexes are not. I
would like to retrieve the column index for a given column name, but haven't
been able to find the answer (i'm sure the real reason I can't find the
answer is because I haven't asked the right question). Any ideas?

thanks
 
G

Guest

Your solution works great. I am also curious if the following is ok to use;

int i = myDataTable.Columns["ColumnName"].Ordinal;


--
dchman


Milosz Skalecki said:
There is a method of DataColumnCollection called IndexOf(string columnName) or
IndexOf(DataColumn). Both ADO 1.1 and ADO 2.0 support it:

int index = myDataTable.Columns.IndexOf("searchedColumnName");
if (index < 0)
{
// not found
}
else
{
DataColumn dc = myDataTable.Columns[index];
// do whatever
}

Hope this helps
--
Milosz


dchman said:
I have a datatable that is created dynamically (in an asp.net 2003
application), so various columns may or may not be present. Consequently,
although the column names may be constant, the column indexes are not. I
would like to retrieve the column index for a given column name, but haven't
been able to find the answer (i'm sure the real reason I can't find the
answer is because I haven't asked the right question). Any ideas?

thanks
 
G

Guest

Hi there again,

Yes you can but remember to handle the case column does not exist in
collection:

int index = -1;
DataColumn column = myDataTable.Columns["ColumnName"];
if (column != null)
index = column.Ordinal;

Regards
--
Milosz


dchman said:
Your solution works great. I am also curious if the following is ok to use;

int i = myDataTable.Columns["ColumnName"].Ordinal;


--
dchman


Milosz Skalecki said:
There is a method of DataColumnCollection called IndexOf(string columnName) or
IndexOf(DataColumn). Both ADO 1.1 and ADO 2.0 support it:

int index = myDataTable.Columns.IndexOf("searchedColumnName");
if (index < 0)
{
// not found
}
else
{
DataColumn dc = myDataTable.Columns[index];
// do whatever
}

Hope this helps
--
Milosz


dchman said:
I have a datatable that is created dynamically (in an asp.net 2003
application), so various columns may or may not be present. Consequently,
although the column names may be constant, the column indexes are not. I
would like to retrieve the column index for a given column name, but haven't
been able to find the answer (i'm sure the real reason I can't find the
answer is because I haven't asked the right question). Any ideas?

thanks
 

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