WPF DataGrid选择第一行

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

只有第一个选项卡会自动显示第一行。每个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();
hhjuly 回答:WPF DataGrid选择第一行

DG22到底是什么?您可能需要DG2。 您还可以为此按钮使用try {} catch {}。

randomForest(predictors,decision)

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

您需要类似的东西:

randomForest(decision~.,data=input)
,

如果选择了一行,我就来获取单元格。如果不是,程序将崩溃。如何停止该程序。我使用了if语句,但是如果未选择任何内容并且程序崩溃,它将不会停止程序。

    private void Btn2_Click(object sender,RoutedEventArgs e)
    {

        var DG2 = dg1.SelectedCells[0];

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

        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);   
    }
,

始终选择一行。设置,SelectionMode + =扩展,SelectionUnit = FullRow 用户可以添加行= False,自动生成列标题= False。 对于dat,我使用了绑定到SQL数据库。 MainWindow.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" 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" 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);
,

更新为上述结果。如果DataGrid为空,则此if语句将阻止程序结束。

    private void Btn2_Click(object sender,RoutedEventArgs e)
    {
        if (dg2.SelectedIndex == -1)
        {
            MessageBox.Show("There is no recipe selected","Error");
            return;
        }

        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);
本文链接:https://www.f2er.com/3157486.html

大家都在问