Binding DataGridView columns in WPF

I want to create DataGridas shown in the image below:

enter image description here

I plan to bind DataGridto a list of class objects. The class that I planned

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 another color property that I can associate directly with the color DataGridin column2. But since I plan to bind DataGridto the list of this object, the new property will be recognized as the column itself. How to avoid this? Any suggestions.

+3
source share
3 answers

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>
+4
<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 = Class1, .

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

, .

+1

Color , rowdataound.

AS Autogenerate Columns = "false"

: , , SrNo Datagrid

, , COLOR Column 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

#.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
    }

}
0

All Articles