HOW TO: Build a dynamic gridView at runtime

Rate It (4)

Last post 03-07-2008 12:28 PM by FidelV. 26 replies.

Sort Posts:

  • Re: HOW TO: Build a dynamic gridView at runtime

    02-11-2007, 11:51 PM
    • Loading...
    • vishwa
    • Joined on 10-12-2006, 4:34 AM
    • Marietta, GA
    • Posts 47

    This is an example on the same line, if you want to give a custom header or formatting to dynamic gridview at run time.

    http://www.vishwamohan.com/ShowArticle.aspx?ArticleID=25

     

     

  • Re: HOW TO: Build a dynamic gridView at runtime

    02-12-2007, 1:12 AM
    • Loading...
    • CSharpSean
    • Joined on 10-21-2006, 5:43 AM
    • Orlando, FL
    • Posts 868
  • Re: HOW TO: Build a dynamic gridView at runtime

    03-08-2007, 4:55 PM
    • Loading...
    • RSewell
    • Joined on 01-31-2006, 3:01 PM
    • Posts 41

    I'm not sure if the following will work for a GridView, but it works for a DataGrid:

    If you'd rather not build the whole DataGrid by hand, prefering to let VS2005 do the grunt work with the drag'n'drop it does so well, but want to adjust a dataGridItem's attributes conditionally, you can do that as well.

    For example, say you're displaying ProductName, UnitPrice and UnitsInStock from the Products table of the Northwind database in a DataGrid.  You want to change the text color of the cells to gray if the Discontinued field is true, and you want to change the background color of the item's row cells to red if the UnitsInStock is less than 10 as a visual cue to the operator to reorder more of those units.

    So, with your datagrid (dgProducts) built on the web page into BoundColumns whose DataFields are ProductName, UnitPrice and UnitsInStock (in that order) and with Discontinued loaded into a fourth BoundColumn (hidden with Visible="false"), you can make those individual changes in the sub that loads your data, immediately after you bind the dataset to the grid's datasource, as follows:

    dgProducts.DataSource = ds
    dgProducts.DataBind()

    Dim itm As DataGridItem
    For Each itm In dgProducts
       If itm.Cells(3).Text = False Then
          itm.Cells(0).ForeColor = Drawing.Color.Gray
          itm.Cells(1).ForeColor = Drawing.Color.Gray
          itm.Cells(2).ForeColor = Drawing.Color.Gray
       End If
       If CInt(itm.Cells(2).Text) < 10 Then
           itm.Cells(2).BackColor = Drawing.Color.Red
       End If
    Next

    I don't know how useful that is to anyone else, but it sure helped me.  I'll have to play around and see if there's a way to do this with a GridView; so far I haven't found the DataView's equivalent to a DataGridItem.

    Filed under: ,
  • Re: HOW TO: Build a dynamic gridView at runtime with Style

    05-17-2007, 7:36 AM
    • Loading...
    • utsavuce
    • Joined on 05-17-2007, 10:15 AM
    • Posts 5

    Hi,

    I am looking forward to build a complete gridview (with row style) as custom control .

    e.g:

    <asp:GridView ID="gridViewLoanDetails" runat="server" CssClass=Label AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical" AllowPaging="True" PageSize="30">

    <Columns>

    <asp:BoundField DataField="month" HeaderText="Month" />

    <asp:BoundField DataField="scheduledBalance" HeaderText="Scheduled Balance" />

    <asp:BoundField DataField="interest" HeaderText="Interest" />

    <asp:BoundField DataField="principalPayed" HeaderText="Principal Payed" />

    <asp:BoundField DataField="emi" HeaderText="EMI" />

    <asp:BoundField DataField="apr" HeaderText="APR" />

    </Columns>

    <FooterStyle BackColor="#CCCCCC" />

    <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />

    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />

    <HeaderStyle BackColor="Black" Font-Bold="True" Font-Size="Small" ForeColor="White" />

    <AlternatingRowStyle BackColor="#CCCCCC" />

    </asp:GridView>

    How one can add Bound field and footer style etc. at runtime?

    Thanks, 

     

  • Re: HOW TO: Build a dynamic gridView at runtime

    05-18-2007, 6:32 AM
    • Loading...
    • sibush
    • Joined on 05-18-2007, 10:28 AM
    • Posts 1

    Sorry its working

  • Crying [:'(] Re: HOW TO: Build a dynamic gridView at runtime

    06-15-2007, 3:38 PM
    • Loading...
    • briosucks
    • Joined on 06-15-2007, 3:46 PM
    • Posts 2
    This is good, however, what if I want to use a different select query? I will have a different data key and that is causing me problems. I fake it out by naming the key with the the same name as the original data grid (in this example it is CustomerID) and it works, or if I create a key for every select query with the same name such as MyKey (replacing CustomerID) and that works. any better suggestions? The problem I run into is when the query has a multi-column key (CutomerID, OrderID, PaymentID). I am looking to tap your smarts. Thanks for your help.
  • Re: HOW TO: Build a dynamic gridView at runtime

    08-24-2007, 8:21 AM

    i used the link above and managed to generate a gridview dynamically with no problems. However....

    i now need to take it a step further and add edit,update,delete funcions to it. Any ideas how?

    www.nursery-net.com
  • Re: HOW TO: Build a dynamic gridView at runtime

    01-15-2008, 11:36 AM
    • Loading...
    • fred51523
    • Joined on 12-18-2007, 9:32 PM
    • Posts 128

    Thanx for the Link "C#SharpSean"

    was looking for C# code .......

    It helped me to create a datagrid dynamically

    Need to know - how to add edit functions

    thanx

     

    Nothing That I Have Is Truly Mine
  • Re: HOW TO: Build a dynamic gridView at runtime

    02-23-2008, 1:31 AM
    • Loading...
    • tapamay@sipl
    • Joined on 02-23-2008, 6:10 AM
    • Kolkata
    • Posts 3

    I am a novice of ASP.NET.Can you please explain in which context dynamic creation of GridView will be useful. 

    Tapamay Ghosh
    Software Developer
    Saltlake Infosolutions Pvt. Ltd.
    Kolkata,West-Bengal,India
    My Company Site: http://www.saltlakesoft.com
  • Re: HOW TO: Build a dynamic gridView at runtime

    02-27-2008, 8:57 AM
    • Loading...
    • lconley
    • Joined on 12-19-2003, 12:10 PM
    • Posts 268

    In my case i am running quality data for pass fail/yields rates for different processes my users requested to have each process have its own "grid"

    depending on which month I do not know how many grids i will need , Jan 4 processes, Feb 2 process March 6 .....

    so I run my query to find out what operations were used for the time frame they selected , then I loop through and create a grid for each operation and fill with data.

    335ICT
    Customer Part No. Passed Failed Total Yield
    ABB 2101059-001 10 0 10 100.00%
    ABB 2101059-002 1 0 1 100.00%
    SIEMS 2020939-001 26 1 27 96.30%
    SIEMS 2020963-002 3 0 3 100.00%
    SLOGX 16-0013SUB 2 0 2 100.00%
    SLOGX 16-0015 3 1 4 75.00%
    Total 45 2 47 95.74%
    BOARD-QC
    Customer Part No. Passed Failed Total Yield
    ABB 2018546-004 10 0 10 100.00%
    Total 10 0 10 100.00%
    FT
    Customer Part No. Passed Failed Total Yield
    ABB 2015636-004 5 0 5 100.00%
    ABB 2018546-004 48 15 63 76.19%
    ASML 4022.471.96242 19 1 20 95.00%
    ASML 4022.471.96302 40 0 40 100.00%
    BRAUN 435178 4 1 5 80.00%
    CYMER 7X10 SERIES CY102116-SUB 3 0 3 100.00%
    CYMER IP CY134210-SUB 3 0 3 100.00%
    CYMER IP CY139255-SUB 3 1 4 75.00%
    CYMER IP CY140904-SUB 3 0 3 100.00%
    CYMER IP CY141555-SUB 13 1 14 92.86%
    CYMER IP CY142677 14 0 14 100.00%
    SIEMS 2015821-001 1 0 1 100.00%
    SIEMS 2020939-001 28 2 30 93.33%
    SIEMS 2020963-002 4 2 6 66.67%
    SLOGX 16-0013SUB 4 0 4 100.00%
    SLOGX 16-0014 1 0 1 100.00%
    Total 193 23 216 89.35%
    SMT-BOTTOM
    Customer Part No. Passed Failed Total Yield
    ABB 2101946-001 28 2 30 93.33%
    ABB 2102080-001 29 1 30 96.67%
    ABB 2102919-001 220 0 220 100.00%
    CYMER IP CY139029 9 0 9 100.00%
    CYMER IP CY141458-SUB 20 0 20 100.00%
    INPUT OUTPUT CIO1448750001 2 0 2 100.00%
    OYOAM 456-02440-07PRO 30 0 30 100.00%
    Total 338 3 341 99.12%
    WAVE
    Customer Part No. Passed Failed Total Yield
    ABB 2012541-002 24 0 24 100.00%
    ABB 2015619-001 20 0 20 100.00%
    ABB 2015636-004 18 0 18 100.00%
    ABB 2017222-004 16 0 16 100.00%
    ABB 2101059-001 5 0 5 100.00%
    ASML 4022.471.96242 20 0 20 100.00%
    CYMER 6X10 SERIES CY06-19166-00 13 1 14 92.86%
    CYMER IP CY123760 12 0 12 100.00%
    CYMER IP CY139029 21 6 27 77.78%
    CYMER IP CY142677 1 0 1 100.00%
    L3 POWER PARAGON 1005109300 0 3 3 0.0000%
    LTX 865-3263-00-SUB 45 0 45 100.00%
    SLOGX 16-0015 1 0 1 100.00%
    THERMA-WAVE 14-017697-SUB 11 0 11 100.00%
    THERMA-WAVE 14-028346-SUB 10 0 10 100.00%
    Total 217 10 227 95.59%
    5DX
    Customer Part No. Passed Failed Total Yield
    CYMER IP CY139029 25 2 27 92.59%
    CYMER IP CY141458-SUB 1 0 1 100.00%
    CYMER IP CY142677 1 0 1 100.00%
    OYOAM 456-02440-07PRO 15 0 15 100.00%
    SIEMS 2020963-002 2 0 2 100.00%
    Total 44 2 46 95.65%
    FINAL QC
    Customer Part No. Passed Failed Total Yield
    ABB 2015619-001 20 0 20 100.00%
    ABB 2015636-004 5 0 5 100.00%
    ABB 2018546-004 50 0 50 100.00%
    ABB 2100299-001 95 0 95 100.00%
    ABB 2101059-001 40 0 40 100.00%
    ABB 2101059-002 80 0 80 100.00%
    ABB 2101946-001 27 0 27 100.00%
    ABB 2102080-001 26 4 30 86.67%
    ASML 4022.471.96242 20 0 20 100.00%
    ASML 4022.471.96302 40 0 40 100.00%
    BRAUN 435178 8 0 8 100.00%