怎么双击datagridview的一行数据,就可以在我的textbox显示它们,然后点修改就可以更新它们?
下面是我的代码,我做了添加的.我想做的这个软件就是有一个datagridview和几个textbox和添加,修改,删除按钮的,连到Access数据库的.请各位教教.谢谢.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;namespace 电子钱包测试版
{
public partial class 电子钱包Form : Form
{
static string cnnstr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\dzqb.mdb;Persist Security Info=True;Jet OLEDB:Database Password=6682152";
static string aaastr = "SELECT 日期,钱包所剩钱,钱柜所剩钱,当天使用数,使用方法 FROM 电子钱包";
static OleDbConnection cnn = new OleDbConnection(cnnstr);
static OleDbDataAdapter aaa = new OleDbDataAdapter(aaastr, cnn);
OleDbCommandBuilder bbb = new OleDbCommandBuilder(aaa);
DataSet sss = new DataSet();
public 电子钱包Form()
{
InitializeComponent();
cnn.Open();
aaa.Fill(sss, "电子钱包");
电子钱包dataGridView.DataSource = sss.Tables["电子钱包"];
cnn.Close();
} private void 添加button_Click(object sender, EventArgs e)
{
cnn.Open();
DataRow thisRow = sss.Tables["电子钱包"].NewRow();
thisRow["日期"] = 日期TextBox.Text.Trim();
thisRow["钱包所剩钱"] = 钱包所剩钱TextBox.Text.Trim();
thisRow["钱柜所剩钱"] = 钱柜所剩钱TextBox.Text.Trim();
thisRow["当天使用数"] = 当天使用数TextBox.Text.Trim();
thisRow["使用方法"] = 使用方法TextBox.Text.Trim();
sss.Tables["电子钱包"].Rows.Add(thisRow);
aaa.Update(sss, "电子钱包");
cnn.Close();
} private void 修改button_Click(object sender, EventArgs e)
{
} private void 电子钱包dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{ } }
}
下面是我的代码,我做了添加的.我想做的这个软件就是有一个datagridview和几个textbox和添加,修改,删除按钮的,连到Access数据库的.请各位教教.谢谢.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;namespace 电子钱包测试版
{
public partial class 电子钱包Form : Form
{
static string cnnstr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\dzqb.mdb;Persist Security Info=True;Jet OLEDB:Database Password=6682152";
static string aaastr = "SELECT 日期,钱包所剩钱,钱柜所剩钱,当天使用数,使用方法 FROM 电子钱包";
static OleDbConnection cnn = new OleDbConnection(cnnstr);
static OleDbDataAdapter aaa = new OleDbDataAdapter(aaastr, cnn);
OleDbCommandBuilder bbb = new OleDbCommandBuilder(aaa);
DataSet sss = new DataSet();
public 电子钱包Form()
{
InitializeComponent();
cnn.Open();
aaa.Fill(sss, "电子钱包");
电子钱包dataGridView.DataSource = sss.Tables["电子钱包"];
cnn.Close();
} private void 添加button_Click(object sender, EventArgs e)
{
cnn.Open();
DataRow thisRow = sss.Tables["电子钱包"].NewRow();
thisRow["日期"] = 日期TextBox.Text.Trim();
thisRow["钱包所剩钱"] = 钱包所剩钱TextBox.Text.Trim();
thisRow["钱柜所剩钱"] = 钱柜所剩钱TextBox.Text.Trim();
thisRow["当天使用数"] = 当天使用数TextBox.Text.Trim();
thisRow["使用方法"] = 使用方法TextBox.Text.Trim();
sss.Tables["电子钱包"].Rows.Add(thisRow);
aaa.Update(sss, "电子钱包");
cnn.Close();
} private void 修改button_Click(object sender, EventArgs e)
{
} private void 电子钱包dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{ } }
}
取得相应行的值
2、dataGridView1_CellContentClick
这个事件能获取点击按钮,在里面写更新的代码就行了,和添加,修改,删除的过程不用说了吧。
dataGridView1.Rows[Index].Cells[列号].Value.ToString(); 取得相应框的值,
private void 电子钱包dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
this.Textbox1.text= this.dataGridView1.Rows[e.RowIndex].Cells[列号].Value.ToString();
}
private void 修改button_Click(object sender, EventArgs e)
{ 这里放个sql更新语句就可以了。
}
2.利用BindingSource的CurrentChange事件,里面编写如下代码 DataTable dataTable1; //我这里是将BindingSource命名为了bsData
private void bsData_CurrentChanged(object sender, EventArgs e)
{
//如果没有绑定到数据源那么直接返回
if (bsData.DataSource == null)
return; DataRow drow = bsData.Current as DataRow; //把DataRow里面的数据写入到你的TextBox里面
//…… } //添加的话
private void Add()
{
//如果没有绑定到数据源那么直接返回
if (bsData.DataSource == null)
return; DataRow drow = (DataRow)bsData.AddNew(); //用TextBox里面的内容去更新DataRow } //删除
private void Delete()
{
//如果没有绑定到数据源那么直接返回
if (bsData.DataSource == null)
return; DataRow drow = bsData.Current as DataRow; drow.Delete(); } //集中进行数据库的写入
private void dbWrite()
{
//利用一个循环检查那些新添加的
foreach (DataRow dr in dataTable1.Rows)
{
//这里的顺序是有讲究的,为什么呢?自己体会,呵呵
//如果是刚刚删除的
if (dr.RowState == DataRowState.Deleted)
{ }
//如果是刚刚添加的
else if (dr.RowState == DataRowState.Added)
{ }
//如果是刚刚修改的
else if (dr.RowState == DataRowState.Modified)
{ }
}
}
不就是简单的增,删,改,查,么
几句话搞定..
Adapter.update(datatable);
DATATABL.AcceptChanges();
-------------------------------------------------------
SqlCommandBuilder sb = new SqlCommandBuilder(Adapter);
上下两种都可以满足需求
至于点击要显示到TEXTBOX..
可以使用SelectionChanged事件.
添加方法. dataGridView.selectRow(0).cells["列名"].tostring();
接你的写法
DataRow[] dr = sss.Tables["电子钱包"].Sleect(“字段名称=12345”)//双击时候在Table中选中这一行
if(dr.lengt==1)//只有一行记录时候更新可以做循环更新
{
dr[0].BeginEdit()
dr[0]["日期"] = 日期TextBox.Text.Trim();
dr[0]["钱包所剩钱"] = 钱包所剩钱TextBox.Text.Trim();
dr[0]["钱柜所剩钱"] = 钱柜所剩钱TextBox.Text.Trim();
dr[0]["当天使用数"] = 当天使用数TextBox.Text.Trim();
dr[0]["使用方法"] = 使用方法TextBox.Text.Trim();
dr[0].EndEdit()
aaa.Update(sss, "电子钱包");
}不知道你是不是想要这样的效果!!