【问题标题】:WPF DataGrid select first rowWPF DataGrid 选择第一行
【发布时间】:2019-11-05 17:46:44
【问题描述】:

我在每个选项卡上使用 TabControl 和 DataGrid。

只有第一个 Tab 会自动显示第一行。每个 DataGrid 都填充了与 SQL 的绑定。当 DataGrid 为空时,或者当没有选择一个字段并单击了选择按钮时,程序就会崩溃。

IsSynchronized = on
SelectionMode = Extended
SelectionUnit = FullRow

我需要选择按钮的正确语句。这是我的代码:

    private void Btn1_Click(object sender, RoutedEventArgs e)
    {
        var DG1 = dg1.SelectedCells[0];
        var cellInfor = dg1.SelectedCells[0];
        dg1.CurrentCell = new DataGridCellInfo(dg1.Items[0], dg1.Columns[0]);
        var DG11 = (cellInfor.Column.GetCellContent(cellInfor.Item) as TextBlock).Text;

        if (dg1 == null || dg1.CurrentCell == null)
        {
            MessageBox.Show("there is no recorid selected");
                return;
        }
    }

    private void Btn2_Click(object sender, RoutedEventArgs e)
    {
        var DG2 = dg2.SelectedItems[0].ToString();

        if (DG2.Items[0].ToString() == null || DG2.SelectedItem[0] == null)
        {
            MessageBox.Show("There is no RecID number selected", "Error");
            return;
        }


        int RecID = Convert.ToInt32(DG22);
        //this.AptzClearControls();
        //this.GetAptzRecipe(RecID);
        //this.DisplayAptzRecipe();

【问题讨论】:

    标签: c# wpf data-binding datagrid


    【解决方案1】:

    最后是DG22?您可能需要 DG2。 您也可以对这个按钮使用 try{} catch{}。

    if (DG2.Items[0].ToString() != null && DG2.SelectedItem[0] != null)
            {
            int RecID = Convert.ToInt32(DG2);
            }
    else 
    {
    MessageBox.Show("There is no RecID number selected", "Error");
    return;
    }
    

    =====================编辑=========== 我不确定你想做什么.. 您需要获取所选单元格的行/列吗?
    看看这个页面:Microsoft manual

    你需要这样的东西:

    private void selectedCellsButton_Click(object sender, System.EventArgs e)
    {
        Int32 selectedCellCount =
            dataGridView1.GetCellCount(DataGridViewElementStates.Selected);
        if (selectedCellCount > 0)
        {
                for (int i = 0; i < selectedCellCount; i++)
                {
                    (dataGridView1.SelectedCells[i].RowIndex.ToString());
                    (dataGridView1.SelectedCells[i].ColumnIndex.ToString());
                }
    
            }
        }
    }
    

    【讨论】:

    • 在 if 语句中,Items 和 SelectedItems 是红色下划线的。我找不到要使用的正确语句。
    • 编辑了我上面的答案。
    【解决方案2】:

    始终选择一行。设置,SelectionMode += 扩展,SelectionUnit = FullRow 用户可以添加行 = False,自动生成列标题 = False。 对于 dat,我使用了 Binding to SQL 数据库。主窗口.xaml 我添加了 DataGridTextColumn 绑定和标题名称。在 MainWindow>xaml.cs 中,我为数据 AddID 选择了第一个单元格。 我希望这对其他人有所帮助。

            <TabItem x:Name="tab2" Header="tab2">
                <Grid Background="#FFE5E5E5" DataContext="{StaticResource addTableViewSource}" >
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <DataGrid x:Name="dg2" HorizontalAlignment="Left" Height="136" Margin="34,29,0,0" VerticalAlignment="Top" Width="500"
                              ItemsSource="{Binding}" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeRows="False" AutoGenerateColumns="False" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="True" RenderTransformOrigin="0.529,0.412" AutomationProperties.IsColumnHeader="True">
                        <DataGrid.Columns>
                            <DataGridTextColumn Binding="{Binding AddID}" Header="AddID" Foreground="#FFBF0E0E"/>
                            <DataGridTextColumn Binding="{Binding FName}" Header="FName"/>
                            <DataGridTextColumn Binding="{Binding LName}" Header="LName"/>
                            <DataGridTextColumn Binding="{Binding Street}" Header="Street" Foreground="#FF18904F"/>
                            <DataGridTextColumn Binding="{Binding City}" Header="City"/>
                            <DataGridTextColumn Binding="{Binding State}" Header="State"/>
                            <DataGridTextColumn Binding="{Binding Zip}" Header="Zip"/>
    
                        </DataGrid.Columns>
    
                    </DataGrid>
    
                    <Button x:Name="btn2" Content="Button" HorizontalAlignment="Left" Margin="273,185,0,0" VerticalAlignment="Top" Width="75" Click="Btn2_Click" RenderTransformOrigin="-2.084,4.366"/>
                </Grid>
            </TabItem>
    
        private void Btn2_Click(object sender, RoutedEventArgs e)
        {
            var DG2 = dg2.SelectedCells[0]; 
            var cellInfor = dg2.SelectedCells[0];
            dg2.CurrentCell = new DataGridCellInfo(dg2.Items[0], dg2.Columns[0]);
            var DG22 = (cellInfor.Column.GetCellContent(cellInfor.Item) as TextBlock).Text;
    
            MessageBox.Show(DG22);
    
             int AddID = Convert.ToInt32(DG22);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-03
      • 2015-07-17
      • 1970-01-01
      • 2011-03-31
      • 2012-06-27
      • 1970-01-01
      相关资源
      最近更新 更多