wpf中怎么利用DataGrid来进行数据库的修改和删除,求指导 datagridwpfC# 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 代码没有。 你是怎么绑定到datagrid上的??使用binding机制了么,如果使用的话,修改与删除也就简单了。操作后台绑定的集合就行了。 MySQLConnection conn = null; conn = new MySQLConnection(new MySQLConnectionString("localhost", "studentsystem", "root", "1234").AsString); conn.Open(); string sql = "select count(*) from stuinfos where UserName ='" + textBox1.Text + "'"; MySQLCommand mda = new MySQLCommand(sql, conn); string s; if (radioButton1.IsChecked == false) { s = (string)radioButton2.Content; } else s = (string)radioButton1.Content; object obj = mda.ExecuteScalar(); if (obj == null || obj == DBNull.Value) { listBox1.Items.Add(textBox1.Text + " 数据已经存在"); MessageBox.Show("添加失败!"); } else if(textBox2.Text==textBox3.Text) { MySQLCommand commn = new MySQLCommand("insert into stuinfos(UserName,Password,Name,Sex,Age,Tel,Email,Address) values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox4.Text + "','" + s + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "') ", conn); listBox1.Items.Add(textBox1.Text + " 成功添加"); if (commn.ExecuteNonQuery() > 0) MessageBox.Show("添加成功!"); else MessageBox.Show("添加失败!"); Login l1 = new Login(); string sql1 = "select * from stuinfos "; MySQLDataAdapter mda1 = new MySQLDataAdapter(sql1, conn); DataSet ds = new DataSet(); mda1.Fill(ds, "table1"); l1.Show(); l1.datagrid.ItemsSource = ds.Tables[0].DefaultView; commn.Dispose(); conn.Close(); }else MessageBox.Show("两次密码不一致!");这个我进行添加的代码,可以加载到数据库,之后跳转回DataGrid也可以。 http://msdn.microsoft.com/zh-cn/magazine/dd252944.aspx 我是想弄的简单点,就是每一条数据后面可以有两个按钮或者是链接进行删除和修改,可是我现在不知道怎么添加这个两个按钮或链接 <DataGridTemplateColumn Header="Operating Items" MinWidth="230" Width="*"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Border Background="White" BorderBrush="White" BorderThickness="1" Margin="-1"> <Button Content="Edit" x:Name="btnEdit" Width="50" Height="28" Margin="5,0,0,0" Style="{StaticResource myBtnStyle1}"></Button> </Border> </DataTemplate> </DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn> 我是想弄的简单点,就是每一条数据后面可以有两个按钮或者是链接进行删除和修改,可是我现在不知道怎么添加这个两个按钮或链接 <DataGridTemplateColumn Header="Operating Items" MinWidth="230" Width="*"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Border Background="White" BorderBrush="White" BorderThickness="1" Margin="-1"> <Button Content="Edit" x:Name="btnEdit" Width="50" Height="28" Margin="5,0,0,0" Style="{StaticResource myBtnStyle1}"></Button> </Border> </DataTemplate> </DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn>我用的是WPF啊,控件中没有这个属性啊</DataGridTemplateColumn.CellTemplate> 添加到 <DataGrid.Columns> </DataGrid.Columns>里。懂?????? 可是按着这个添加按钮,他报错了,"在“System.Windows.Markup.StaticResourceHolder”上提供值时引发了异常。" Style="{StaticResource myBtnStyle1}" 这句 去掉。 C# 线程池 如何中继所有线程? mvc entity 调用存储过程问题 发现在一个万分奇怪的多线程问题 求一自动运行的清除SQL2005事务日志的东东 winform怎么播放声音?我用了API来播放。当时他要的声音文件的全部地址 C#开发"图形化操作售票"请教,同时包含并发性处理的问题.欢迎讨论.高分赏赐! socket传送文件问题 大家帮忙看看这句话,怎么写啊!!? 谁有?进了看看 请教一个组合算法的实现问题 类库和UI相互引用了?依赖倒置? List的筛选
conn = new MySQLConnection(new MySQLConnectionString("localhost", "studentsystem", "root", "1234").AsString);
conn.Open();
string sql = "select count(*) from stuinfos where UserName ='" + textBox1.Text + "'";
MySQLCommand mda = new MySQLCommand(sql, conn);
string s;
if (radioButton1.IsChecked == false)
{
s = (string)radioButton2.Content;
}
else s = (string)radioButton1.Content;
object obj = mda.ExecuteScalar();
if (obj == null || obj == DBNull.Value)
{
listBox1.Items.Add(textBox1.Text + " 数据已经存在");
MessageBox.Show("添加失败!");
}
else if(textBox2.Text==textBox3.Text)
{
MySQLCommand commn = new MySQLCommand("insert into stuinfos(UserName,Password,Name,Sex,Age,Tel,Email,Address) values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox4.Text + "','" + s + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "') ", conn);
listBox1.Items.Add(textBox1.Text + " 成功添加");
if (commn.ExecuteNonQuery() > 0) MessageBox.Show("添加成功!");
else MessageBox.Show("添加失败!");
Login l1 = new Login();
string sql1 = "select * from stuinfos ";
MySQLDataAdapter mda1 = new MySQLDataAdapter(sql1, conn);
DataSet ds = new DataSet();
mda1.Fill(ds, "table1");
l1.Show();
l1.datagrid.ItemsSource = ds.Tables[0].DefaultView;
commn.Dispose();
conn.Close();
}else
MessageBox.Show("两次密码不一致!");
这个我进行添加的代码,可以加载到数据库,之后跳转回DataGrid也可以。
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Border Background="White" BorderBrush="White" BorderThickness="1" Margin="-1">
<Button Content="Edit" x:Name="btnEdit" Width="50" Height="28" Margin="5,0,0,0" Style="{StaticResource myBtnStyle1}"></Button>
</Border>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Border Background="White" BorderBrush="White" BorderThickness="1" Margin="-1">
<Button Content="Edit" x:Name="btnEdit" Width="50" Height="28" Margin="5,0,0,0" Style="{StaticResource myBtnStyle1}"></Button>
</Border>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
我用的是WPF啊,控件中没有这个属性啊</DataGridTemplateColumn.CellTemplate>