开发者

adding row dyanamicaly to gridview in WPF

开发者 https://www.devze.com 2023-02-03 16:30 出处:网络
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.

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>


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?"));
    }
0

精彩评论

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