在我的应用程序中,我需要获取行的ID列内容,单击一个复选框。 目前我正在使用CheckBox_Click和datagrid_SelectionChanged,但它不能正常工作。问题是此处理程序仅在选择更改后触发,并且会导致计数正确的问题。我尝试了不同的处理程序,但我只能使这一个工作(因为它工作)。 我可以在代码中更改哪些内容或应该使用哪个事件处理程序?
的SelectionChanged:
private void datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//somecode
try
{
DataGrid dataGrid = sender as DataGrid;
DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(dataGrid.SelectedIndex);
if (row != null)
{
DataGridCell RowColumn = dataGrid.Columns[1].GetCellContent(row).Parent as DataGridCell;
string CellValue = ((TextBlock)RowColumn.Content).Text;
int i = Convert.ToInt32(CellValue);
//somecode
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
//somecode
} here
checkboxclick:
private void CheckBox_Click_1(object sender, RoutedEventArgs e) //Klikniecie w checkboxa
{
//somecode
if (((CheckBox)sender).IsChecked == true)
{
//somecode
}
else
{
//somecode
}
//somecode
}
XAML:
<DataGrid x:Name="dataGrid" ItemsSource="{Binding}" AutoGenerateColumns="False" Margin="0" Grid.Row="2" OverridesDefaultStyle="True" IsManipulationEnabled="True" CanUserAddRows="false" SelectionChanged="datagrid_SelectionChanged">
<DataGrid.ItemBindingGroup>
<BindingGroup/>
</DataGrid.ItemBindingGroup>
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>
<CheckBox Content="All" Click="CheckBoxAll_Click" x:Name="headerCheckBox" Checked="headerCheckBox_Checked"/>
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="chk" Margin="10 0 0 0" ClickMode="Press" Click="CheckBox_Click_1" IsChecked="{Binding IsChecked, ElementName=headerCheckBox, Mode=OneWay}" PreviewMouseLeftButtonDown="chk_PreviewMouseLeftButtonDown" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding Path=id}" Header="ID" Width="auto" />
//Some columns
</DataGrid.Columns>
</DataGrid>
感谢您的帮助:)
修改 更多代码。我使用“怪异”绑定的方法:D
public void Aktualizuj()
{
try
{
if (aktywnatabela != "old" && aktywnatabela != "cust") { aktywnatabela = "aktywne"; }
string selectStr = ("select * from " + aktywnatabela + " order by id asc");
SQLiteDataAdapter myAdapter = new SQLiteDataAdapter(selectStr, Start.m_dbConnection);
DataSet dset = new DataSet();
int i = myAdapter.Fill(dset);
dataGrid.ItemsSource = dset.Tables[0].DefaultView;
//MessageBox.Show(dset.GetXml().ToString());
for (int x = 2; x <= 10; x++)
{
if (aktywnatabela == "cust")
dataGrid.Columns[x].Visibility = Visibility.Collapsed;
else
dataGrid.Columns[x].Visibility = Visibility.Visible;
}
for (int x = 11; x <= 17; x++)
{
if (aktywnatabela == "cust")
dataGrid.Columns[x].Visibility = Visibility.Visible;
else
dataGrid.Columns[x].Visibility = Visibility.Collapsed;
}
}
catch (SQLiteException ex)
{
MessageBox.Show(ex.Message);
}
}
EDIT2: 我要澄清我的问题 - 这样几乎一切都运行正常 - 除了我已经使用了_SelectionChanged所以只有当我改变选择时它才会触发,当我没有CheckBox检查不起作用时。
答案 0 :(得分:0)
当chekbox为true时,为什么你不在数据网格上使用函数和instert数据
public void FunctionName()
{
DataSet ds = new DataSet();
System.Data.OleDb.OleDbConnection conn = new
System.Data.OleDb.OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Your Path DataBase";
conn.Open();
string sql = "SELECT * FROM YourTable where Id ='" + YourID + "' ;";
OleDbDataAdapter dataadapter = new OleDbDataAdapter(sql, conn);
dataadapter.Fill(ds, "YourTable");
conn.Close();
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "YourTable";
}
你调用函数
private void CheckBox_Click_1(object sender, RoutedEventArgs e) //Klikniecie w checkboxa
{
//somecode
if (((CheckBox)sender).IsChecked == true)
{
FunctionName
}
else
{
//somecode
}
//somecode
}
答案 1 :(得分:0)
你的itemsource对我来说似乎很奇怪但是如果它实际上在你的数据网格中显示数据你可以这样做:
首先在复选框中添加标记:
<CheckBox Name="chk" Margin="10 0 0 0" ClickMode="Press" Click="CheckBox_Click_1" IsChecked="{Binding IsChecked, ElementName=headerCheckBox, Mode=OneWay}" PreviewMouseLeftButtonDown="chk_PreviewMouseLeftButtonDown" Tag={Binding }/>
在点击事件中检索您的代码:
private void CheckBox_Click_1(object sender, RoutedEventArgs e) {
// With the tag you retrieve the object of the list used in DataTemplate
// the list is the binding you did on the itemsSource datagrid
YourObject yourObject = (YourObject) ((CheckBox) sender).Tag;
// Now you have retreive your entire object in the yourObject variable
// You can retreive the specific id or other variable of your object and use it as you want
yourObject.id;
}