tags:

views:

21

answers:

3

Hi,

I am using data grid view in Windows Form standalone application to display items as excel spread sheet in VB.NET. I got a table named CostTypes with column names [CostTypeID, CostType] and values [1,External] and [2,Internal] (These are constant but more values can be added to table).

I want to create columns with names of the values[External , Internal] in DataGridView. If I use databiding directly I get columns [CostTypeID,CostType] which is not what I am looking for.

If some one could explain how to create columns at runtime in datagridview or how to retrieve data from data base using LINQ so that [External , Internal] turn out to be columns that would be great.

Thanks in advance.

A: 

You could use a listView an display items in one row

Something like this

 <asp:ListView ID="NewsLV" runat="server">
    <ItemTemplate>
      <td id="Td1" runat="server" valign="top" style="width: 33%;">
        <asp:Label ID="titleLabel" runat="server" Text='<%# Eval("CostType") %>' CssClass="GridTitle" />
        <br />
        <div style="padding: 2px;">
          <asp:Label ID="descriptionLabel" runat="server" Text='<%# Eval("CostTypeID") %>'
            Font-Size="X-Small" />
        </div>
      </td>
    </ItemTemplate>
    <LayoutTemplate>
      <table id="Table1" runat="server" border="0" style="">
        <tr id="itemPlaceholderContainer" runat="server">
          <td id="itemPlaceholder" runat="server">
          </td>
        </tr>
      </table>
    </LayoutTemplate>
  </asp:ListView>
alejandrobog
Sorry I should have mentioned it. Its a Windows app not web application
fireBand
A: 

What you could do is create a Sub that will create the columns for you. There is so much control you have when you do it that way. Here is an example:

With DataGridView1
       .AutoGenerateColumns = False

        Dim columnCostType As New DataGridViewTextBoxColumn
        With columnCostType
             .HeaderText = "Cost Type"
             .DataPropertyName = CustomObject.CostType
             .Name = CustomObject.CostType
        End With
        .Columns.Add(columnCostType)
End With
xenosyde