2010-04-22 62 views
2

我在Windows窗體獨立應用程序中使用數據網格視圖來顯示項目作爲Excel中的電子表格在VB.NET中。我得到一個名爲CostTypes的表,名稱爲[CostTypeID,CostType]和值[1,External]和[2,Internal](這些是常量,但可以將更多值添加到表中)。從表值創建DataGridView列

我想要在DataGridView中創建名稱爲[External,Internal]的列。如果我直接使用數據庫,我會得到不是我正在尋找的列[CostTypeID,CostType]。

如果有人能夠解釋如何在運行時在datagridview中創建列,或者如何使用LINQ從數據庫中檢索數據,這樣[External,Internal]會變成很棒的列。

在此先感謝。

回答

0

你可以使用一個ListView的顯示項目一排

像這樣的事情

<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

對不起,我應該提到它。它是Windows應用程序而不是Web應用程序 – fireBand 2010-04-22 15:13:51

0

你可以做的就是創建一個子,將創建列給你的。當你這樣做時,你有太多的控制。這裏是一個例子:

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