如何在WPF中向DataGrid添加行?

时间:2019-04-21 11:44:47

标签: wpf datagrid

我正在尝试在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>

2 个答案:

答案 0 :(得分:1)

班级

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>

用于设置数据网格的项目来源的代码。

myDataGrid.ItemsSource = Foo.myList;

ScreenShot enter image description here

答案 1 :(得分:0)

在这里,我首先注意到DataGrid名称错误myDataGrid,并且要显示空行,所以将绑定设置为null。它显示为空 如果你要绑定数组,你可以像这样使用绑定

 <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来绑定整个集合