我无法让我的复选框列工作。我想要实现的是在应用程序加载时检查复选框,如果条件与表中的值匹配,并在每次选中或取消选中复选框时向数据库发送更新命令。
我该怎么写它才能让它起飞?我希望在没有MVVM的情况下实现我的目标。如果有人可以帮我解开,我将不胜感激。
这是我有多远:
<DataGrid>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Audited" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox x:Name="cBox" Checked="DataGridCheckBoxColumn_Checked" Unchecked="DataGridCheckBoxColumn_Unchecked"
IsChecked="{Binding Audited, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<CheckBox x:Name="cBox" Checked="DataGridCheckBoxColumn_Checked" Unchecked="DataGridCheckBoxColumn_Unchecked"
IsChecked="{Binding Audited, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding Location}" Header="Location" Visibility="Collapsed"/>
<DataGridTextColumn Binding="{Binding Date, StringFormat=MM-dd-yy}" Header="Date"/>
</DataGrid.Columns>
</DataGrid>
xaml.cs
public MainWindow()
{
InitializeComponent();
string connectionString = "datasource=; Port=; Username=; Password=";
string sMonth = DateTime.Now.ToString("MM");
string sYear = DateTime.Now.ToString("yyyy");
string sDate = DateTime.Now.ToString("yyyy-MM-dd");
MySqlConnection connection = new MySqlConnection(connectionString);
MySqlCommand Audit = new MySqlCommand("Select Audited from Daily.Table where MONTH(Date) = @sMonth AND YEAR(Date) = @sYear", connection);
Audit.Parameters.Add(new MySqlParameter("sDate", sDate));
try
{
connection.Open();
MySqlDataReader AuditR = Audit.ExecuteReader();
while (AuditR.Read())
{
if (AuditR["Audited"] != DBNull.Value)
{
//How I can set the checkbox to checked?
};
}
AuditR.Close();
AuditR.Dispose();
private void DataGridCheckBoxColumn_Checked(object sender, RoutedEventArgs e)
{
string connectionString = "datasource=; Port=; Username=; Password=";
MySqlConnection connection = new MySqlConnection(connectionString);
MySqlCommand AuditUpdate = new MySqlCommand("update Daily.Table set Audited='Yes' where ID= '" + this.txtID.Text + "'", connection);
CheckBox checkBox = sender as CheckBox;
//How can I mark the checkbox as checked from here?
}
private void DataGridCheckBoxColumn_Unchecked(object sender, RoutedEventArgs e)
{
string connectionString = "datasource=; Port=; Username=; Password=";
MySqlConnection connection = new MySqlConnection(connectionString);
MySqlCommand AuditUpdate = new MySqlCommand("update Daily.Table set Audited=NULL where ID= '" + this.txtID.Text + "'", connection);
CheckBox checkBox = sender as CheckBox;
//How can I mark the checkbox as unchecked from here?
}
答案 0 :(得分:1)
我遇到了类似的问题。我没有使用MVVM解决了它。这样的事情可能对你有帮助。
<DataGridTextColumn Header="Operand" Binding="{Binding Path=MyVal}">
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="{x:Type CheckBox}">
<EventSetter Event="SelectionChanged" Handler="CheckBox_SelectionChanged" />
</Style>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
创建一个包含datagridview
行的类class MyRowItem
{
public string ID {get;set;}
public Checkbox Audited {get;set;}
public string Location {get;set;}
public string Date {get;set;}
}
创建MyRowItem列表并将所有行存储在那里
List<MyRowItem> rowList = new List<MyRowItem>();
rowList.Add(new MyRowItem(){ Audited = yourvalue, Location = "yourvalue", Date = "yourvalue"});
现在在Checkbox_SelectionChanged事件处理程序
中private void Checkbox_SelectionChanged(object sender, RoutedEventArgs e)
{
string connectionString = "datasource=; Port=; Username=; Password=";
MySqlConnection connection = new MySqlConnection(connectionString);
MySqlCommand AuditUpdate = new MySqlCommand("update Daily.Table set Audited='"Yes"' where ID= '" + this.txtID.Text + "'", connection);
CheckBox checkBox = sender as CheckBox;
foreach(MyRowItem mri in rowList)
{
if(mri.ID == "your matching ID")
{
//something like this
mri.Audited = checkBox;
}
}
}
最后,
Datagrid.Source = rowList;
Datagrid.Refresh();