Solved

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

Posted on 2004-10-25
913 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
Question by:mbosch
    11 Comments
     
    LVL 8

    Expert Comment

    by:daffodils
    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
    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

          <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
     
    LVL 3

    Expert Comment

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

    Author Comment

    by:mbosch
    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
    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
    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
    ((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:
    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
    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
    Why did I get a B?
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    875 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now