DataGridView Column binding in WPF

后端 未结 3 744
死守一世寂寞
死守一世寂寞 2021-01-02 21:59

I want to design a DataGrid as shown in the picture below:

\"enter

<
相关标签:
3条回答
  • 2021-01-02 22:35
    <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.

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

    Hope this helps.

    0 讨论(0)
  • 2021-01-02 22:38

    You can set AutoGenerateColumns to False and 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.

    <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 讨论(0)
  • 2021-01-02 22:41

    You want to replace the Colour property value to another Colour Property value it can be done using rowdatabound.

    AS you set Autogenerate Columns = "false"

    Columns : Index , Colour , Location and SrNo will display in 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 :

    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
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题