Home All Groups Group Topic Archive Search About

DataTable Column Index for Given ColumnName

Author
24 Feb 2007 4:17 PM
dchman
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
--
dchman

Author
24 Feb 2007 6:21 PM
Milosz Skalecki [MCAD]
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;
    }
}

--
Milosz


Show quote
"sfx_p***@hotmail.com" wrote:

> Hi All,
>
> In my repeater control I have a <div> that is set to run at the server
> with the intention that I can hide it based on certain criteria.
>
> I'm able to do this OK but I'm not sure I have the most efficent
> method.  Heres what I have so far (simplified of course!).  Does
> anybody know a better way?????
>
> In the aspx...
>
>         <asp:Repeater ID="rptTwoSpecials" runat="server">
>             <ItemTemplate>
>                 <div id="co2" runat="server">CO2/km - <%#
> DataBinder.Eval(Container.DataItem,"co2") %></div>
>             </ItemTemplate>
>         </asp:Repeater>
>
> An the code behind (Page_Load event)...
>
>          foreach (RepeaterItem rptTemp in rptTwoSpecials.Items)
>          {
>              ((HtmlGenericControl)rptTemp.FindControl("co2")).Visible
> = false;
>          }
>
> Thanks guys!
>
>
Author
24 Feb 2007 6:48 PM
Milosz Skalecki [MCAD]
apologies, i posted answer to another post :-)
--
Milosz


Show quote
"Milosz Skalecki [MCAD]" wrote:

> 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;
>     }
> }
>
> --
> Milosz
>
>
> "sfx_p***@hotmail.com" wrote:
>
> > Hi All,
> >
> > In my repeater control I have a <div> that is set to run at the server
> > with the intention that I can hide it based on certain criteria.
> >
> > I'm able to do this OK but I'm not sure I have the most efficent
> > method.  Heres what I have so far (simplified of course!).  Does
> > anybody know a better way?????
> >
> > In the aspx...
> >
> >         <asp:Repeater ID="rptTwoSpecials" runat="server">
> >             <ItemTemplate>
> >                 <div id="co2" runat="server">CO2/km - <%#
> > DataBinder.Eval(Container.DataItem,"co2") %></div>
> >             </ItemTemplate>
> >         </asp:Repeater>
> >
> > An the code behind (Page_Load event)...
> >
> >          foreach (RepeaterItem rptTemp in rptTwoSpecials.Items)
> >          {
> >              ((HtmlGenericControl)rptTemp.FindControl("co2")).Visible
> > = false;
> >          }
> >
> > Thanks guys!
> >
> >
Author
24 Feb 2007 6:26 PM
Milosz Skalecki [MCAD]
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


Show quote
"dchman" wrote:

> 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
> --
> dchman
Author
24 Feb 2007 6:48 PM
dchman
thanks - that does the trick.
--
dchman


Show quote
"Milosz Skalecki [MCAD]" wrote:

> 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" wrote:
>
> > 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
> > --
> > dchman
Author
24 Feb 2007 8:21 PM
dchman
Your solution works great.  I am also curious if the following is ok to use;

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


--
dchman


Show quote
"Milosz Skalecki [MCAD]" wrote:

> 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" wrote:
>
> > 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
> > --
> > dchman
Author
25 Feb 2007 5:57 PM
Milosz Skalecki [MCAD]
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


Show quote
"dchman" wrote:

> Your solution works great.  I am also curious if the following is ok to use;
>
> int i = myDataTable.Columns["ColumnName"].Ordinal;
>
>
> --
> dchman
>
>
> "Milosz Skalecki [MCAD]" wrote:
>
> > 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" wrote:
> >
> > > 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
> > > --
> > > dchman

AddThis Social Bookmark Button