如何在WPF中向DataGrid添加行?

问题描述 投票:0回答:2

我正在尝试在DataGrid中添加一行。我希望每个项目都是一个字符串。这就是我尝试过的:

string[] testRow = { "hi", "h2", "h3", "h4" };
myDataGrid.Items.Add(testRow );

在做了一些研究后,我尝试制作ListBox项目并尝试了这个:

ListBoxItem user = new ListBoxItem();
user.Content = "hi";

ListBoxItem bname = new ListBoxItem();
bname.Content = "hi2";

ListBoxItem city = new ListBoxItem();
city.Content = "hi3";

ListBoxItem text = new ListBoxItem();
text.Content = "hi4";

ListBoxItem[] row = { user, bname, city, text };

ListBoxItem[] row = { user, bname, city, text };
myDataGrid.Items.Add(row);

经过更多的研究,我尝试使用DataGridRow,但没有成功......

DataGridRow rowt = new DataGridRow();

我有4个数据列。我的列被定义为DataGridTextColumns。我的行成功添加,但结果只是空白:enter image description here

我到底做错了什么?

编辑:

根据要求:

                    <DataGrid x:Name="myDataGrid" HorizontalAlignment="Left" Height="579" VerticalAlignment="Top" Width="611" Margin="0,0,-1.286,-1.857" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" AutoGenerateColumns="False">
                        <DataGrid.Columns>
                            <DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="User Name"/>
                            <DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="Business"/>
                            <DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="City"/>
                            <DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="Text"/>
                        </DataGrid.Columns>
                    </DataGrid>
wpf datagrid
2个回答
2
投票

public class Foo
{
    public string UserName { get; set; }
    public string Business { get; set; }
    public string City { get; set; }
    public string Text { get; set; }

    //Our List
    public static List<Foo> myList = new List<Foo>()
    {
        new Foo() { UserName = "Username1", Business = "Business1", City = "City1", Text = "Text1" },
        new Foo() { UserName = "Username2", Business = "Business2", City = "City2", Text = "Text2" },
        new Foo() { UserName = "Username3", Business = "Business3", City = "City3", Text = "Text3" },
        new Foo() { UserName = "Username4", Business = "Business4", City = "City4", Text = "Text4" },
        new Foo() { UserName = "Username5", Business = "Business5", City = "City5", Text = "Text5" },
        new Foo() { UserName = "Username6", Business = "Business6", City = "City6", Text = "Text6" }
    };
}

DataGrid绑定时要小心。

    <DataGrid x:Name="myDataGrid" AutoGenerateColumns="False" IsReadOnly="True">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding UserName}" ClipboardContentBinding="{x:Null}" Header="User Name"/>
            <DataGridTextColumn Binding="{Binding Business}" ClipboardContentBinding="{x:Null}" Header="Business"/>
            <DataGridTextColumn Binding="{Binding City}" ClipboardContentBinding="{x:Null}" Header="City"/>
            <DataGridTextColumn Binding="{Binding Text}" ClipboardContentBinding="{x:Null}" Header="Text"/>
        </DataGrid.Columns>
    </DataGrid>

用于设置datagrid的itemssource的代码。

myDataGrid.ItemsSource = Foo.myList;

ScreenShot enter image description here


0
投票

在这里我注意到首先是DataGrid名称是错误的myDataGrid和来显示空行你设置绑定为null所以。它显示为空,如果你是wana绑定数组你可以使用这样的绑定

 <DataGrid x:Name="myDataGrid"  HorizontalAlignment="Left" Height="579" VerticalAlignment="Top" Width="611" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" AutoGenerateColumns="False">


 <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding [0]}" ClipboardContentBinding="{x:Null}" Header="User Name"/>
            <DataGridTextColumn Binding="{Binding [0]}" ClipboardContentBinding="{x:Null}" Header="Business"/>
            <DataGridTextColumn Binding="{Binding [0]}" ClipboardContentBinding="{x:Null}" Header="City"/>
            <DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="Text"/>
        </DataGrid.Columns>
    </DataGrid>




string[] testRow = { "hi", "h2", "h3", "h4" };
            myDataGrid.Items.Add(testRow);

注意你可以使用ItemsSource来绑定整个集合

© www.soinside.com 2019 - 2024. All rights reserved.