WPF 中的 DataGridView 列绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21898291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataGridView Column binding in WPF
提问by user1687824
I want to design a DataGridas shown in the picture below:
我想设计一个DataGrid如下图所示:


I am planning to bind the DataGridto list of objects of a class. The class I was planning is
我打算将 绑定DataGrid到类的对象列表。我计划的课程是
class Class1
{
public Int32 Index { get; set; }
public string Colour { get; set; }
public string Location { get; set; }
public string Srno { get; set; }
}
I have a problem. I would like to have one more colour property which I can bind directly to the colour of the DataGridin column2. But since I am planning to set DataGridbinding to list of this object, the new property will be recognized as a column itself. How do I avoid that? Any suggetions.
我有个问题。我想要多一个颜色属性,我可以将它直接绑定到DataGridcolumn2 中的颜色。但是由于我打算设置DataGrid绑定到此对象的列表,因此新属性将被识别为列本身。我该如何避免?任何建议。
回答by Rohit Vats
You can set AutoGenerateColumnsto Falseand take responsibility in your hand to provide the list of columns you want to avoid auto generation of columns when DataSource or DataMember properties are set.
您可以将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>
回答by Max Mazur
<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>
This is how you would do it, if you set the Datagrid1.ItemsSource = a List of Class1, like so.
如果您像这样设置 Datagrid1.ItemsSource = Class1 的列表,这就是您的方法。
List<Class1> myList = new List<Class1>();
DataGrid1.ItemsSource = myList;
Hope this helps.
希望这可以帮助。
回答by Kapil
You want to replace the Colour property value to another Colour Property value it can be done using rowdatabound.
您想将颜色属性值替换为另一个颜色属性值,这可以使用 rowdatabound 来完成。
AS you set Autogenerate Columns = "false"
当您设置 Autogenerate Columns = "false"
Columns : Index , Colour , Location and SrNo will display in Datagrid
列:索引、颜色、位置和 SrNo 将显示在 Datagrid 中
you are saying you have another colour who's value should replace COLOUR Column in the datagrid.If I am correct you can do this by the following...
你是说你有另一种颜色的价值应该替换数据网格中的颜色列。如果我是正确的,你可以通过以下方式做到这一点......
<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 :
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 :
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
}
}

