C#中datagrid绑定数据库问题
我是新手,最近制作一个C#的工程使用了datagrid控件,将其显示Access数据库中的数据表点击每一条,会在页面上相应显示出其具体信息。但对其修改删除后,不能立即在d...
我是新手,最近制作一个C#的工程
使用了datagrid控件,将其显示Access数据库中的数据表
点击每一条,会在页面上相应显示出其具体信息。
但对其修改删除后,不能立即在datagrid空间中看到,必须要重新查询才可以!
听说这时要数据绑定,请问该怎么做?
private void button1_Click(object sender, System.EventArgs e)//修改
{string sql="";
DataSet Mydata1 = new DataSet();
if(this.textBox1.Text=="")
{
MessageBox.Show("请先选择要修改的记录","提示");
}
else
{
sql = "Update CheckMachineInfo Set MachinName="+"'"+this.textBox2.Text+"'"+","+"MachineCode="+"'"+this.textBox3.Text+"'"+","+"MachineID="+"'"+this.textBox6.Text+"'"+","+"MachineStatus="+"'"+this.comboBox1.Text+"'"+"where ID="+this.textBox1.Text;
OleDbConnection myConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+AppDomain.CurrentDomain.BaseDirectory.ToString()+"test.MDB");
myConnection.Open();
OleDbCommand myCommand = new OleDbCommand(sql, myConnection);
myCommand.ExecuteReader();
myConnection.Close();
MessageBox.Show("修改成功!");
}
}
private void Form20_Load(object sender, System.EventArgs e)
{
string sql="";
OleDbConnection myConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+AppDomain.CurrentDomain.BaseDirectory.ToString()+"test.MDB");
DataSet Mydata1 = new DataSet();
OleDbDataAdapter Myadapter = new OleDbDataAdapter(sql,myConnection);
myConnection.Open();
OleDbCommand myCommand = new OleDbCommand(sql, myConnection);
OleDbDataReader myReader = myCommand.ExecuteReader();
if(myReader.Read())
{m=0; while(myReader.Read())
{
m++;
}
//MessageBox.Show(m.ToString());
myReader.Close();
Myadapter.Fill(Mydata1,"CheckMachineInfo");
dataGrid1.DataSource = Mydata1.Tables["CheckMachineInfo"];
myConnection.Close();
if(m=0)
{ this.textBox1.Text = dataGrid1[0,0].ToString();//
this.textBox2.Text = dataGrid1[0,1].ToString();//
this.textBox3.Text = dataGrid1[0,2].ToString();//
this.textBox6.Text = dataGrid1[0,3].ToString();//
this.comboBox1.Text = dataGrid1[0,4].ToString();// }
}
else
{ //MessageBox.Show("null");
label10.Text = "没有查到符合条件的纪录";
myReader.Close();
myConnection.Close();
}
} 展开
使用了datagrid控件,将其显示Access数据库中的数据表
点击每一条,会在页面上相应显示出其具体信息。
但对其修改删除后,不能立即在datagrid空间中看到,必须要重新查询才可以!
听说这时要数据绑定,请问该怎么做?
private void button1_Click(object sender, System.EventArgs e)//修改
{string sql="";
DataSet Mydata1 = new DataSet();
if(this.textBox1.Text=="")
{
MessageBox.Show("请先选择要修改的记录","提示");
}
else
{
sql = "Update CheckMachineInfo Set MachinName="+"'"+this.textBox2.Text+"'"+","+"MachineCode="+"'"+this.textBox3.Text+"'"+","+"MachineID="+"'"+this.textBox6.Text+"'"+","+"MachineStatus="+"'"+this.comboBox1.Text+"'"+"where ID="+this.textBox1.Text;
OleDbConnection myConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+AppDomain.CurrentDomain.BaseDirectory.ToString()+"test.MDB");
myConnection.Open();
OleDbCommand myCommand = new OleDbCommand(sql, myConnection);
myCommand.ExecuteReader();
myConnection.Close();
MessageBox.Show("修改成功!");
}
}
private void Form20_Load(object sender, System.EventArgs e)
{
string sql="";
OleDbConnection myConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+AppDomain.CurrentDomain.BaseDirectory.ToString()+"test.MDB");
DataSet Mydata1 = new DataSet();
OleDbDataAdapter Myadapter = new OleDbDataAdapter(sql,myConnection);
myConnection.Open();
OleDbCommand myCommand = new OleDbCommand(sql, myConnection);
OleDbDataReader myReader = myCommand.ExecuteReader();
if(myReader.Read())
{m=0; while(myReader.Read())
{
m++;
}
//MessageBox.Show(m.ToString());
myReader.Close();
Myadapter.Fill(Mydata1,"CheckMachineInfo");
dataGrid1.DataSource = Mydata1.Tables["CheckMachineInfo"];
myConnection.Close();
if(m=0)
{ this.textBox1.Text = dataGrid1[0,0].ToString();//
this.textBox2.Text = dataGrid1[0,1].ToString();//
this.textBox3.Text = dataGrid1[0,2].ToString();//
this.textBox6.Text = dataGrid1[0,3].ToString();//
this.comboBox1.Text = dataGrid1[0,4].ToString();// }
}
else
{ //MessageBox.Show("null");
label10.Text = "没有查到符合条件的纪录";
myReader.Close();
myConnection.Close();
}
} 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询