我目前正在处理需要在数据网格中的复选框上进行点击事件的项目。
手动生成所有列和行。 datagrid的最后一列包含复选框,我希望用户检查这个。当我要检查框时,会发生异常:
PresentationFramework.dll中出现未处理的“System.InvalidOperationException”类型异常
其他信息:此视图不允许使用'EditItem'。
现在,是否需要任何单独的点击事件来处理点击事件。
XAML:
<DataGrid x:Name="single_payment" HorizontalAlignment="Left" ColumnWidth="*" Margin="11,87,0,0" VerticalAlignment="Top" Height="134" Width="261" BorderThickness="0" AutoGenerateColumns="False"/>
C#:
/* setting columns */
DataGridTextColumn c1 = new DataGridTextColumn();
c1.Header = "#";
//c1.IsReadOnly = true;
c1.Width = 10;
c1.Binding = new Binding("Num");
single_payment.Columns.Add(c1);
DataGridTextColumn c2 = new DataGridTextColumn();
c2.Header = "Name";
//c1.IsReadOnly = true;
c2.Binding = new Binding("Start");
single_payment.Columns.Add(c2);
DataGridTextColumn c4 = new DataGridTextColumn();
c4.Header = "Amount";
//c4.IsReadOnly = true;
c4.Binding = new Binding("Amt");
single_payment.Columns.Add(c4);
DataGridCheckBoxColumn c3 = new DataGridCheckBoxColumn();
c3.Header = "Paid";
//c3.IsReadOnly = false;
c3.Binding = new Binding("Finich");
single_payment.Columns.Add(c3);
/* Set columns ends here */
/* set row */
try
{
dbconnection c = new dbconnection();
c.cmd.CommandText = "SELECT * FROM purpose_master WHERE type='otp';";
c.da.Fill(c.dt);
if (c.dt.Rows.Count > 0)
{
for (int col = 0; col < c.dt.Rows.Count; col++)
{
//MessageBox.Show(col.ToString());
single_payment.Items.Add(new Item() { Num = c.dt.Rows[col][0].ToString(), Start = c.dt.Rows[col][1].ToString(), Amt = c.dt.Rows[col][4].ToString(), Finich = "" });
}
}
c.con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
/* set row end */
答案 0 :(得分:2)
通常,Checkbox具有可能在其列(DataGridCheckBoxColumn
)中公开的Checked和UnChecked事件。如果您需要复选框控件的单击事件,建议您添加模板列而不是复选框列,并在DataTemplate
中添加复选框。提高其中的Click事件并使用它..