Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-10-25
11
Medium Priority
?
921 Views
Last Modified: 2010-05-19
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
0
Comment
Question by:mbosch
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 8

Expert Comment

by:daffodils
ID: 12404777
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
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 12404800
Are you using VS.NET? Its really very easy to format your datagrid in the VS .NET datagrid designer.

Best, Nauman.
0
 
LVL 3

Expert Comment

by:nitrogenx
ID: 12405112

      <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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 3

Expert Comment

by:nitrogenx
ID: 12405126
oops, use daffodil's answer
0
 
LVL 1

Author Comment

by:mbosch
ID: 12413909
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
 
LVL 3

Expert Comment

by:nitrogenx
ID: 12416607
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
 
LVL 3

Expert Comment

by:nitrogenx
ID: 12416624
or by column number:

        ((BoundColumn)GridView1.Columns[0]).DataFormatString = "{0:c}";
        ((BoundColumn)GridView1.Columns[1]).DataFormatString = "{0:p}";
0
 
LVL 3

Expert Comment

by:nitrogenx
ID: 12416665
((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
 
LVL 3

Accepted Solution

by:
nitrogenx earned 375 total points
ID: 12416696
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
 
LVL 3

Expert Comment

by:nitrogenx
ID: 12416711
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
 
LVL 3

Expert Comment

by:nitrogenx
ID: 12485699
Why did I get a B?
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question