开发者

How to insert into a column in data grid?

<DataGrid Width="300" Height="200" Name="datagrid">
    <DataGrid.Columns>
        <DataGridTextColumn Header="artists" />
        <开发者_开发百科;DataGridTextColumn Header="albums" />
    </DataGrid.Columns>
</DataGrid>

In cs file i have:

string artist_s = "something";

I want to insert artist_s string into artists column.


DataGrids don't take values for specific columns, you have items, one item represents one row. Please see the documentation for more information and some examples.

You could add an entry which just does not set all properties though, e.g.

<DataGrid Name="testGrid">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Name}"/> <!-- Those bindings are needed to display the data -->
        <DataGridTextColumn Binding="{Binding Occupation}"/>
    </DataGrid.Columns>
</DataGrid>
testGrid.Items.Add(new { Name = "John" });
testGrid.Items.Add(new { Occupation = "Programmer" });
testGrid.Items.Add(new { Name = "Skeet", Occupation = "Übermensch" });

Which produces:

How to insert into a column in data grid?

As you can see each item has its own row.


If you do not know how bindings work, read this article on MSDN.


Try:

<DataGrid Width="300" Height="200" Name="datagrid">
        <DataGrid.Columns>
            <DataGridTextColumn Header="artists" Binding="{Binding artist_s}" />
            <DataGridTextColumn Header="albums" />
        </DataGrid.Columns>
</DataGrid>

This is assuming you have your data context defined already.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜