Professional Web Applications Themes

DataGrid Columns - ASP.NET Data Grid Control

I have a DataGrid that I use to display comments fields from a database. I have the columns wrap. One column that I want to allow to have more space, is all scrunched up. I have tried style sheets, <ItemStyle> with no luck. So, does anyone have any ideas as to how to force the grid to properly set the size of the columns? for ex: Column1 | Column2 | Column3 (width=30%) | Column4 width(50%) | Column5 | This does not work for me. Thanks, Brian...

  1. #1

    Default DataGrid Columns

    I have a DataGrid that I use to display comments fields from a database. I
    have the columns wrap. One column that I want to allow to have more space,
    is all scrunched up. I have tried style sheets, <ItemStyle> with no luck.
    So, does anyone have any ideas as to how to force the grid to properly set
    the size of the columns?

    for ex:
    Column1 | Column2 | Column3 (width=30%) | Column4 width(50%) | Column5 |

    This does not work for me.

    Thanks,

    Brian


    bspann Guest

  2. #2

    Default Datagrid Columns

    I have a datagrid that contains a bunch of boundcolumns:

    <asp:datagrid id="DataGrid1" runat="server" autogeneratecolumns="False">
    <columns>
    <asp:boundcolumn datafield="Column1" sortexpression=" Column1" />
    <asp:boundcolumn datafield="Column2" sortexpression=" Column2" />
    <asp:boundcolumn datafield="Column3" sortexpression=" Column3" />
    ...
    </columns>
    </datagrid>

    On the ItemDataBound event for the DataGrid I need to return the index of
    the column with a specific key (key is the name of the column in the
    database that the column is databound to - ie "Column1", "Column2", etc).

    I thought this would be very easy to do - just iterate through the
    DataGrid1.Columns collection - however I can't find any properties of the
    DataGridColumn that corresponds to the name of the column that it's bound
    to.

    Any ideas? Am I looking in the wrong place - wrong approach?

    Any help or advice is very appreciated - thanks in advance,
    J. Shane Kunkle
    [email]jkunklevt.edu[/email]



    J. Shane Kunkle Guest

  3. #3

    Default Re: Datagrid Columns

    I always use column header text as a key.

    Eliyahu

    "J. Shane Kunkle" <shanecaudillweb.com> wrote in message
    news:OMMd9s6rEHA.2904TK2MSFTNGP15.phx.gbl...
    > I have a datagrid that contains a bunch of boundcolumns:
    >
    > <asp:datagrid id="DataGrid1" runat="server" autogeneratecolumns="False">
    > <columns>
    > <asp:boundcolumn datafield="Column1" sortexpression=" Column1" />
    > <asp:boundcolumn datafield="Column2" sortexpression=" Column2" />
    > <asp:boundcolumn datafield="Column3" sortexpression=" Column3" />
    > ...
    > </columns>
    > </datagrid>
    >
    > On the ItemDataBound event for the DataGrid I need to return the index of
    > the column with a specific key (key is the name of the column in the
    > database that the column is databound to - ie "Column1", "Column2", etc).
    >
    > I thought this would be very easy to do - just iterate through the
    > DataGrid1.Columns collection - however I can't find any properties of the
    > DataGridColumn that corresponds to the name of the column that it's bound
    > to.
    >
    > Any ideas? Am I looking in the wrong place - wrong approach?
    >
    > Any help or advice is very appreciated - thanks in advance,
    > J. Shane Kunkle
    > [email]jkunklevt.edu[/email]
    >
    >
    >

    Eliyahu Goldin Guest

  4. #4

    Default Re: Datagrid Columns

    Thank you for your response.



    I thought about this kind of approach - I could key off of the column Header
    (and I also could use the sortexpression I suppose) but I would like to
    avoid making these restrictions when designing this control. There is no
    guarantee that uses of this control will name the column header text or the
    sort expression the same as the bound database column id.



    "Eliyahu Goldin" <removemeegoldinmonarchmed.com> wrote in message
    news:%23kYUI96rEHA.2016TK2MSFTNGP12.phx.gbl...
    >I always use column header text as a key.
    >
    > Eliyahu
    >
    > "J. Shane Kunkle" <shanecaudillweb.com> wrote in message
    > news:OMMd9s6rEHA.2904TK2MSFTNGP15.phx.gbl...
    >> I have a datagrid that contains a bunch of boundcolumns:
    >>
    >> <asp:datagrid id="DataGrid1" runat="server" autogeneratecolumns="False">
    >> <columns>
    >> <asp:boundcolumn datafield="Column1" sortexpression=" Column1" />
    >> <asp:boundcolumn datafield="Column2" sortexpression=" Column2" />
    >> <asp:boundcolumn datafield="Column3" sortexpression=" Column3" />
    >> ...
    >> </columns>
    >> </datagrid>
    >>
    >> On the ItemDataBound event for the DataGrid I need to return the index of
    >> the column with a specific key (key is the name of the column in the
    >> database that the column is databound to - ie "Column1", "Column2", etc).
    >>
    >> I thought this would be very easy to do - just iterate through the
    >> DataGrid1.Columns collection - however I can't find any properties of the
    >> DataGridColumn that corresponds to the name of the column that it's bound
    >> to.
    >>
    >> Any ideas? Am I looking in the wrong place - wrong approach?
    >>
    >> Any help or advice is very appreciated - thanks in advance,
    >> J. Shane Kunkle
    >> [email]jkunklevt.edu[/email]
    >>
    >>
    >>
    >
    >

    J. Shane Kunkle Guest

  5. #5

    Default Re: Datagrid Columns

    DataField property of BoundColumn class is what you are looking for.

    Eliyahu

    "J. Shane Kunkle" <shanecaudillweb.com> wrote in message
    news:OMMd9s6rEHA.2904TK2MSFTNGP15.phx.gbl...
    > I have a datagrid that contains a bunch of boundcolumns:
    >
    > <asp:datagrid id="DataGrid1" runat="server" autogeneratecolumns="False">
    > <columns>
    > <asp:boundcolumn datafield="Column1" sortexpression=" Column1" />
    > <asp:boundcolumn datafield="Column2" sortexpression=" Column2" />
    > <asp:boundcolumn datafield="Column3" sortexpression=" Column3" />
    > ...
    > </columns>
    > </datagrid>
    >
    > On the ItemDataBound event for the DataGrid I need to return the index of
    > the column with a specific key (key is the name of the column in the
    > database that the column is databound to - ie "Column1", "Column2", etc).
    >
    > I thought this would be very easy to do - just iterate through the
    > DataGrid1.Columns collection - however I can't find any properties of the
    > DataGridColumn that corresponds to the name of the column that it's bound
    > to.
    >
    > Any ideas? Am I looking in the wrong place - wrong approach?
    >
    > Any help or advice is very appreciated - thanks in advance,
    > J. Shane Kunkle
    > [email]jkunklevt.edu[/email]
    >
    >
    >

    Eliyahu Goldin Guest

  6. #6

    Default Datagrid columns

    Isn't there a way to refer to a column in a datagrid by the name of the
    column, rather than the index? This is a dynamically built datagrid, and
    when I refer to dg.Column(0), it works fine, but if I refer to
    dg.Column("ID"), I get an error saying the string format is incorrect. But
    I don't always know what the column number is so I have to refer to it by
    name:


    With dg
    .AutoGenerateColumns = "False"
    .CellPadding="4"
    .BackColor=Color.White
    .BorderWidth = Unit.Pixel(3)
    .GridLines=GridLines.Horizontal
    .BorderStyle=BorderStyle.Double
    .ShowHeader = true
    .HeaderStyle.BackColor = Color.Black
    .HeaderStyle.ForeColor = Color.White
    .HeaderStyle.Font.Bold = true
    .EditItemStyle.BackColor = Color.Silver
    End With

    Dim DGBoundColumnID as New BoundColumn
    With DGBoundColumnID
    .Visible=False
    .DataField="ID"
    .ReadOnly=True
    End With
    dg.Columns.Add(DGBoundColumnID)



    Rock Guest

  7. #7

    Default Re: Datagrid columns

    Write a simple method that will take the column name as a parameter, loop
    through the Column collection and return the column index.

    Eliyahu

    "Rock" <rockisland> wrote in message
    news:OJ9QcFr7EHA.2192TK2MSFTNGP14.phx.gbl...
    > Isn't there a way to refer to a column in a datagrid by the name of the
    > column, rather than the index? This is a dynamically built datagrid, and
    > when I refer to dg.Column(0), it works fine, but if I refer to
    > dg.Column("ID"), I get an error saying the string format is incorrect.
    But
    > I don't always know what the column number is so I have to refer to it by
    > name:
    >
    >
    > With dg
    > .AutoGenerateColumns = "False"
    > .CellPadding="4"
    > .BackColor=Color.White
    > .BorderWidth = Unit.Pixel(3)
    > .GridLines=GridLines.Horizontal
    > .BorderStyle=BorderStyle.Double
    > .ShowHeader = true
    > .HeaderStyle.BackColor = Color.Black
    > .HeaderStyle.ForeColor = Color.White
    > .HeaderStyle.Font.Bold = true
    > .EditItemStyle.BackColor = Color.Silver
    > End With
    >
    > Dim DGBoundColumnID as New BoundColumn
    > With DGBoundColumnID
    > .Visible=False
    > .DataField="ID"
    > .ReadOnly=True
    > End With
    > dg.Columns.Add(DGBoundColumnID)
    >
    >
    >

    Eliyahu Goldin Guest

Similar Threads

  1. How can I sum Columns on a datagrid?
    By Luis Esteban Valencia in forum ASP.NET Data Grid Control
    Replies: 7
    Last Post: June 15th, 04:45 AM
  2. Add columns to datagrid on the fly!
    By VBTECH in forum ASP.NET Data Grid Control
    Replies: 1
    Last Post: April 21st, 08:51 AM
  3. Replies: 0
    Last Post: April 5th, 03:58 AM
  4. Datagrid with SQL keywords on their columns
    By Luis Esteban Valencia in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: October 11th, 05:56 PM
  5. Columns and Inherited Datagrid...Active Schema does not support columns
    By rob thomson in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: September 4th, 03:09 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139