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