MANIPULATING AUTOGENERATED DATAGRID COLUMNS... WHERE, WHEN, HOW?

Hi experts,
I have a datagrid that but have the columns autogenerated.  At what point can I perform changes on the columns of the data?  For example, if I need to make one column an hyperlink column, and another column be formatted as currency, and yet another column be formatted as percent (I think you get the point).  How do I access these properties of the datagrid columns?

m
LVL 1
mboschAsked:
Who is Participating?
 
nitrogenxConnect With a Mentor Commented:
if you actually need to add columns programattically do so like the following:

Aspx:

        <asp:DataGrid ID=DataGrid1 Runat=server></asp:DataGrid>

Code:

        HyperLinkColumn HyperLinkColumn1 = new HyperLinkColumn();
        HyperLinkColumn1.NavigateUrl = "http://www.foobar.com";

        BoundColumn BoundColumn1 = new BoundColumn("ColumnName");
        BoundColumn1.DataFormatString = "{0:c}";

        DataGrid1.Columns.Add(HyperLinkColumn1);
        DataGrid1.Columns.Add(BoundColumn1);
0
 
daffodilsCommented:
You would need to set the DataGrid property AutoGenerateColumns="False" and manually select the columns to display.

To format a column as percent or currency, you can use the "DataFormatString" property:
<asp:BoundColumn DataField="OrderAmount" HeaderText="OrderAmount" DataFormatString="{0:c}"></asp:BoundColumn>
<asp:BoundColumn DataField="ProfitPercent" HeaderText="Profit Percent" DataFormatString="{0:p}"></asp:BoundColumn>

For a Hyperlink or Button.. add the appropriate columns:
<asp:HyperLinkColumn Text="Go To" Target="_self" DataNavigateUrlField="CustomerID" NavigateUrl="www.foo.com"></asp:HyperLinkColumn>
<asp:ButtonColumn Text="Show Details" CommandName="Select"></asp:ButtonColumn>

You can use the "Property Builder" dialog for the DataGrid control from the property page in Visual Studio .NET.. that makes it easier to add columns and perform formatting etc.
0
 
nauman_ahmedCommented:
Are you using VS.NET? Its really very easy to format your datagrid in the VS .NET datagrid designer.

Best, Nauman.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
nitrogenxCommented:

      <asp:TemplateColumn HeaderText="UserID">
       <ItemTemplate>
         <%# string.Format("${0:#.00}",DataBinder.Eval(Container.DataItem, "Money").ToString()) %>
       </ItemTemplate>
      </asp:TemplateColumn>
      <asp:TemplateColumn HeaderText="UserID">
       <ItemTemplate>
         <%# string.Format("{0}%",DataBinder.Eval(Container.DataItem, "Money").ToString()) %>
       </ItemTemplate>
      </asp:TemplateColumn>


0
 
nitrogenxCommented:
oops, use daffodil's answer
0
 
mboschAuthor Commented:
I know I can do it like this.  But I have to be able to do it programmatically since the columns resulting may change and some have 20 columns or more.  For instance, I know that everything in the 4 column will be currency, but I don't know (don't care) what the others are.  And I know that column 10 will be a hyperlink with the NavigateURL = whatever... Thanks!
0
 
nitrogenxCommented:
Aspx:

<asp:BoundColumn DataField="OrderAmount" HeaderText="OrderAmount">
</asp:BoundColumn>
<asp:BoundColumn DataField="ProfitPercent" HeaderText="Profit Percent">
</asp:BoundColumn>

Code:

((BoundColumn)DataGrid1.FindControl("OrderAmount")).DataFormatString = "{0:c}";
((BoundColumn)DataGrid1.FindControl("ProfitPercent")).DataFormatString = "{0:p}";
0
 
nitrogenxCommented:
or by column number:

        ((BoundColumn)GridView1.Columns[0]).DataFormatString = "{0:c}";
        ((BoundColumn)GridView1.Columns[1]).DataFormatString = "{0:p}";
0
 
nitrogenxCommented:
((HyperLinkColumn)GridView1.Columns[3]).NavigateUrl = "http://www.something.com";

or if it is an HTML field such as OnChanged or something you can do:

((CheckBoxField)GridView1.Columns[1]).Attributes.Add("OnChanged","alert('you clicked a check box!')");
0
 
nitrogenxCommented:
Now that I think about it more, I'm not 100% sure that FindControl will work with programattically inserted elements, then you'll have to use indicies and use IndexOf(HyperLinkColumn1) if you do not know the specific index.
0
 
nitrogenxCommented:
Why did I get a B?
0
All Courses

From novice to tech pro — start learning today.