Creating DataGridView Columns from Table Values

I am using a Data Grid View in a standalone Windows Form to display items as an extended spread sheet in VB.NET. I got a table named CostTypes with column names [CostTypeID, CostType] and values ​​[1, External] and [2, Internal] (they are constant, but you can add more values ​​to the table).

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

If someone can explain how to create columns at runtime in datagridview or how to fetch data from database using LINQ so that [External, Internal] turns out to be columns that would be large.

Thanks in advance.

+2


a source to share


3 answers


Find the answer here.

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.columns.aspx



thanks

0


a source


You can use listView display items in one line

Something like that



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

      

0


a source


What you can do is create a Sub that will create columns for you. You have so much control when you do this. Here's 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

      

0


a source







All Articles