简体   繁体   English

WPF DataGrid 未在第一行更新

[英]WPF DataGrid not updating on first row

I have a datagrid like that:我有一个这样的datagrid

<DataGrid Margin="10" x:Name="datagrid1" Height="450" Width="1100">
                                <DataGrid.Columns>
                                    <DataGridTextColumn Width="200" Header="Starttime" Binding="{Binding xxx1}" />
                                    <DataGridTextColumn Width="130" Header="Enddtime" Binding="{Binding xxx2}" />
                                    <DataGridTextColumn Width="130" Header="Gesamte Pause" Binding="{Binding xxx3}" />                                        
                                    <DataGridTemplateColumn Width="130">
                                        <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                            <Button x:Name="button1" Width="80" Click="button1_Click">Urlaub/Feiertag</Button>
                                        </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
                                    </DataGridTemplateColumn>                                   
                                </DataGrid.Columns>                                  
                            </DataGrid>

Datagrid is filled from local .MDF Database like that Datagrid 是从本地 .MDF 数据库中填充的

            string connString2 = @"Data Source=(localdb)\MSSQLLocalDB; AttachDbFilename=|DataDirectory|\LocalDatabase.mdf; Integrated Security=True;";
        SqlConnection MyConn = new SqlConnection(connString2);
        MyConn.Open();
        sqldatadapter1.SelectCommand = new SqlCommand("select * from mytable", MyConn);
        cmbl = new SqlCommandBuilder(sqldatadapter1);
        sqldatadapter1.Fill(table1);
        bindingSource1.DataSource = table1;
        datagrid1.ItemsSource = table1.DefaultView;

I now try to change specific rows by clicking the button1 that I implemented with DataGridTemplateColumn for each row.我现在尝试通过单击我用 DataGridTemplateColumn 为每一行实现的 button1 来更改特定的行。

  private void button1_Click(object sender, RoutedEventArgs e)
    {
        try
        {               
            DataRowView row = (DataRowView)datagrid1.SelectedItem;

            if (row[1].ToString() == "xxx1")
            {
                row[1] = "";
            }
            else
            {
                row[1] = "xxx1";
            }
         
            sqldatadapter1.Update((DataTable)bindingSource1.DataSource);
            CollectionViewSource.GetDefaultView(datagrid1.ItemsSource).Refresh();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }

However this works just fine for all rows except the first one.但是,这对除第一行之外的所有行都有效。 The text of the first row in my datagrid is changed like it is supposed to but my database entries are not updated for this specific row.我的数据网格中第一行的文本已按预期进行更改,但我的数据库条目并未针对该特定行进行更新。 For the second row everything works fine.对于第二行,一切正常。

I found a workaround for myself.我为自己找到了解决方法。 I just added a blank row for the first and last entry.我刚刚为第一个和最后一个条目添加了一个空白行。 After that I just hid those two rows with a Rowstyle like that:之后,我只是用这样的 Rowstyle 隐藏了这两行:

<DataGrid.RowStyle>
                                    <Style TargetType="DataGridRow">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding starttime}" Value="leerzeile">
                                                <Setter Property="Visibility" Value="Collapsed" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </DataGrid.RowStyle>

Not perfect but working.不完美但工作。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM