2014-02-20 45 views
3

結合我想設計一個DataGrid如下圖所示的畫面:的DataGridView列在WPF

enter image description here

我打算到DataGrid綁定到一個類的對象名單。我正在計劃的課程是

class Class1 
{ 
    public Int32 Index { get; set; } 
    public string Colour { get; set; } 
    public string Location { get; set; } 
    public string Srno { get; set; } 
} 

我遇到了問題。我想有一個顏色屬性,我可以直接綁定到第2欄中的DataGrid的顏色。但由於我打算將DataGrid綁定到此對象的列表,所以新屬性將被識別爲列本身。我如何避免這種情況?任何暗示。

+0

在網格中將AutoGenerateColumns設置爲false。 –

回答

4

您可以將AutoGenerateColumns設置爲False,並手頭負責提供您希望避免在設置DataSource或DataMember屬性時自動生成列的列表的列表。

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding SourceCollection}"> 
    <DataGrid.Columns> 
    <DataGridTextColumn Binding="{Binding Index}"/> 
    <DataGridTextColumn Binding="{Binding Colour}"/> 
    <DataGridTextColumn Binding="{Binding Location}"/> 
    <DataGridTextColumn Binding="{Binding Srno}"/> 
    </DataGrid.Columns> 
</DataGrid> 
0

要將Color屬性值替換爲另一個Color屬性值,可以使用rowdatabound來完成。

當您設置自動生成列=「假」

列:指數,顏色,位置和SrNo將在DataGrid中顯示

你說你有另一種顏色是誰的價值應更換color列在DataGrid 。如果我是正確的,你可以通過以下操作...

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding SourceCollection}"> 
     <DataGrid.Columns> 
     <DataGridTextColumn Binding="{Binding Index}"/> 
     <asp:TemplateField HeaderText="Colour" SortExpression="Colour"> 
      <HeaderStyle Wrap="False" /> 
      <ItemStyle Wrap="False" /> 
      <ItemTemplate> 
      <asp:Label ID="lblColor" Text='<%# Bind("Colour") %>' runat="server"></asp:Label> 
      </ItemTemplate> 
      </asp:TemplateField> 
     <DataGridTextColumn Binding="{Binding Location}"/> 
     <DataGridTextColumn Binding="{Binding Srno}"/> 
     </DataGrid.Columns> 
    </DataGrid> 

VB.net:

Protected Sub GridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView.RowDataBound 
      If e.Row.RowType = DataControlRowType.Header Or e.Row.RowType = DataControlRowType.DataRow Then 
      End If 

      If e.Row.RowType = DataControlRowType.DataRow Then 
       Dim lblColor1 As Label 

       lblColor1 = TryCast(e.Row.FindControl("lblColor"), Label) 

       lblColor1.Text = dtData.Rows(e.row.rowindex).ItemArray(0).tostring() ' 
' ItemArray Defined the Column Position. here give your Another Colour Column Value 
      End If 

     End Sub 

C#.NET:

protected void GridView_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.Header | e.Row.RowType == DataControlRowType.DataRow) { 
    } 

    if (e.Row.RowType == DataControlRowType.DataRow) { 
     Label lblColor1 = default(Label); 

     lblColor1 = e.Row.FindControl("lblColor") as Label; 

     lblColor1.Text = dtData.Rows(e.Row.RowIndex).ItemArray(0).tostring(); 
     // 
     // ItemArray Defined the Column Position. here give your Another Colour Column Value 
    } 

} 
1
<DataGrid Name="DataGrid1" AutoGenerateColumns="False"> 
       <DataGrid.Columns> 
        <DataGridTextColumn Header="Index" Binding="{Binding Path=Index}" /> 
        <DataGridTextColumn Header="Colour" Binding="{Binding Path=Colour}"/> 
        <DataGridTextColumn Header="Location" Binding="{Binding Path=Location}" /> 
        <DataGridTextColumn Header="Srno" Binding="{Binding Path=Srno}" /> 
       </DataGrid.Columns> 
      </DataGrid> 

這是你會怎麼做,如果你設置了Datagrid1.ItemsSource = 1類的列表,像這樣。

List<Class1> myList = new List<Class1>(); 
DataGrid1.ItemsSource = myList; 

希望這會有所幫助。