在WPF中动态添加行到gridview

发布于 2024-10-12 02:46:10 字数 2079 浏览 1 评论 0原文

请帮助我编写以下代码,我想将用户输入的行添加到网格视图中。 我可以添加一行,但它是空的!请帮忙。它可以在 Windows 窗体中运行,但不能在 WPF 中运行。

private void button1_Click(object sender, RoutedEventArgs e)
        {
            GetGridView();
        }
        private void GetGridView()
        {

      string[] row0 = {textBox1.Text,"Beatles" };

            dataGrid1.Items.Add(row0); 
            dataGrid1.Columns[0].DisplayIndex = 0;
            dataGrid1.Columns[1].DisplayIndex = 1;

     }

/////////////////////////// 当然,就是这里

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="964">
    <Grid>
        <DataGrid AutoGenerateColumns="False" Height="274" HorizontalAlignment="Left" Margin="509,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="239" DataContext="{Binding}" ItemsSource="{Binding}" ItemStringFormat="{Binding}" SelectedIndex="-1" SelectionChanged="dataGrid1_SelectionChanged">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Header1" />
            <DataGridTextColumn Header="Header" />
        </DataGrid.Columns>
    </DataGrid>
    <TextBox Height="23" HorizontalAlignment="Left" Margin="184,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" TextChanged="textBox1_TextChanged" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="184,187,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="184,125,0,0" Name="textBox3" VerticalAlignment="Top" Width="120" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="184,66,0,0" Name="textBox4" VerticalAlignment="Top" Width="120" />
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="414,231,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>

Please help me with the following code,I want to add a row inputted by user to a gridview.
I am able to add a row but its empty!!Please help.it worked in windows forms but its not working with WPF.

private void button1_Click(object sender, RoutedEventArgs e)
        {
            GetGridView();
        }
        private void GetGridView()
        {

      string[] row0 = {textBox1.Text,"Beatles" };

            dataGrid1.Items.Add(row0); 
            dataGrid1.Columns[0].DisplayIndex = 0;
            dataGrid1.Columns[1].DisplayIndex = 1;

     }

//////////////
sure,here it is

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="964">
    <Grid>
        <DataGrid AutoGenerateColumns="False" Height="274" HorizontalAlignment="Left" Margin="509,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="239" DataContext="{Binding}" ItemsSource="{Binding}" ItemStringFormat="{Binding}" SelectedIndex="-1" SelectionChanged="dataGrid1_SelectionChanged">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Header1" />
            <DataGridTextColumn Header="Header" />
        </DataGrid.Columns>
    </DataGrid>
    <TextBox Height="23" HorizontalAlignment="Left" Margin="184,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" TextChanged="textBox1_TextChanged" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="184,187,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="184,125,0,0" Name="textBox3" VerticalAlignment="Top" Width="120" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="184,66,0,0" Name="textBox4" VerticalAlignment="Top" Width="120" />
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="414,231,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海夜未眠 2024-10-19 02:46:10

编辑:您绑定了 DataGrid 的 ItemsSource,在这种情况下您无法将项目添加到网格本身,将项目添加到绑定的集合(这是我最初建议的)


我不会建议您做类似的事情。在 WPF 中,您应该 绑定您的控件到数据,这样您就可以更改源集合,并且网格将自动更新,这比使用任何接受 类型的输入的方法(例如 DataGrid.Items.Add )更简洁对象。

例如
XAML:

    <DataGrid ItemsSource="{Binding GridData}" Name="DGrid"/>
    <TextBox Name="TB" Width="100"/>
    <Button Content="Add" Click="Button_Click"/>

代码:

    private ObservableCollection<Employee> gridData = new ObservableCollection<Employee>();
    public ObservableCollection<Employee> GridData
    {
        get { return gridData; }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        GridData.Add(new Employee(TB.Text, "Beatles?"));
    }

Edit: You bound the ItemsSource of the DataGrid, you cannot add items to the grid itself while that is the case, add the items to the bound collection (which is what i originally suggested)


I would not suggest you do anything like that. In WPF you should bind your controls to the data, that way you can change the source-collection and the grid will get updated automatically, which is less messy than using any method like DataGrid.Items.Add which accepts input of type object.

e.g.
Xaml:

    <DataGrid ItemsSource="{Binding GridData}" Name="DGrid"/>
    <TextBox Name="TB" Width="100"/>
    <Button Content="Add" Click="Button_Click"/>

Code:

    private ObservableCollection<Employee> gridData = new ObservableCollection<Employee>();
    public ObservableCollection<Employee> GridData
    {
        get { return gridData; }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        GridData.Add(new Employee(TB.Text, "Beatles?"));
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文