Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF数据网格选择第一行_C#_Wpf_Data Binding_Datagrid - Fatal编程技术网

C# WPF数据网格选择第一行

C# WPF数据网格选择第一行,c#,wpf,data-binding,datagrid,C#,Wpf,Data Binding,Datagrid,我在每个选项卡上使用一个TabControl和一个DataGrid 只有第一个选项卡自动显示第一行。每个DataGrid都由SQL绑定填充。当DataGrid为空,或者未选择字段并单击select按钮时,程序崩溃 IsSynchronized = on SelectionMode = Extended SelectionUnit = FullRow 我需要选择按钮的正确语句。这是我的代码: private void Btn1_Click(object sender, RoutedEve

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

只有第一个选项卡自动显示第一行。每个DataGrid都由SQL绑定填充。当DataGrid为空,或者未选择字段并单击select按钮时,程序崩溃

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();

最后是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;
}
===============================编辑=========== 我不知道你想干什么。。 您需要获取所选单元格的行/列吗?
请查看此页面:

你需要这样的东西:

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

        }
    }
}
private void selectedCellsButton\u单击(对象发送者,System.EventArgs e)
{
Int32选择的细胞计数=
dataGridView1.GetCellCount(DataGridViewElementState.Selected);
如果(selectedCellCount>0)
{
对于(int i=0;i
始终选择一行。设置,SelectionMode+=Extended,SelectionUnit=FullRow 用户可以添加行=False,自动生成列标题=False。 对于dat,我使用绑定到SQL数据库。MainWindow.xaml 我添加了DataGridTextColumn绑定和标题名。在MainWindow>xaml.cs中,我为添加的数据选择了第一个单元格。 我希望这能帮助别人

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

私有无效Btn2_单击(对象发送者,路由目标e)
{
var DG2=DG2.所选单元格[0];
var cellInfor=dg2.选定的单元格[0];
dg2.CurrentCell=新的DataGridCellInfo(dg2.Items[0],dg2.Columns[0]);
var DG22=(cellInfor.Column.GetCellContent(cellInfor.Item)作为TextBlock);
MessageBox.Show(DG22);
int AddID=Convert.ToInt32(DG22);

在if语句中,Items和SelectedItems为红色未对齐。我找不到要使用的正确语句。编辑了我的上述答案。