WinForm的DataGrid用法问题,请高手帮忙(Web转Form,发现无所适从) 我也是web转win哎~~~~~~~~~~~~~~~困惑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在form1中 的 datagrid中添加这个行就可以比较容易 来一贴先获得DATAGRID行里的唯一字段,也就是你数据记录的唯一字段,下面的方法是得到值,你可以看看 private void dataGridView_CurrentCellChanged(object sender, System.EventArgs e) { SqlConnection con = new SqlConnection(ConStr); this.txtId.Text = dataGridView[Convert.ToInt32(dataGridView.CurrentCell.RowNumber),0].ToString(); this.dataGridView.Select(dataGridView.CurrentCell.RowNumber); string query_sql = "select * from accounttype where id = '" + txtId.Text + "'"; SqlDataAdapter da = new SqlDataAdapter(query_sql,con); DataSet ds = new DataSet(); da.Fill(ds,"accounttype"); this.txtTempid.DataBindings.Clear(); this.txtTempid.DataBindings.Add("Text",ds,"AccountType.Id"); }下面是把获得的唯一字段传给你的From2这时候要注意了,怎么才能把值传过去 private void btnUpdate_Click(object sender, System.EventArgs e) { if(txtId.Text != "") {//用你Form2的构造函数接收这个值,然后在根据这个值把字段全部绑定到控件上,后来你就可以做更新或者别的什么了,呵呵 FrmUpdate frmupdate = new FrmUpdate(txtId.Text); this.Close(); str_id = this.txtId.Text; frmupdate.ShowDialog(); } else { MessageBox.Show("请选择记录进行修改!","提示",MessageBoxButtons.OK,MessageBoxIcon.Question); } } 就是,我以为不会太难。先重新查询数据库,填充到当前DATAGRID中,然后查询要定位的行就可以啦 tryhttp://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp 这个比较复杂了如果保存原来的行道是简单,用个变量 保存原来的 位置即可。//一下以这种榜定方式为例dataGrid1.DataSource =ds.Tables[0]; //取得当前绑定上下文的位置int psoindex = [Form1].BindingContext[ds.Tables[0]].Position//弹出 Form2 更新数据略.............//Form2 返回后[Form1].ds.Tables[0].Clear()[Form1].da.Fill(ds)[Form1].BindingContext[ds.Tables[0]].Position = psoindex/*建议 方一个 DataView 在窗口上那样 还能取得用户的 排序信息dv.table= [Form1].ds.Tables[0]dataGrid1.DataSource = dvint psoindex = [Form1].BindingContext[dv].Position*/不过如果 多用户时 原来选的那行被 其他用户删除了如和 处理就要你自己考虑了 非法字符替换 winform 打印尺寸 求高效的去重方法 fastreport异步打印报异常 StringBuilder 这次让我很头疼. xtragrid里面怎么加一列按钮啊,而且是图片格式的按钮? 如何获取某个文件所在文件夹的名称 HTTP请求乱码问题 什么是中间键啊? 关于委托的用法问题 中国证监会会歌 各位大侠新春好!请帮我解释一下Page_Unload的问题
在form1中 的 datagrid中添加这个行就可以比较容易
private void dataGridView_CurrentCellChanged(object sender, System.EventArgs e)
{
SqlConnection con = new SqlConnection(ConStr);
this.txtId.Text = dataGridView[Convert.ToInt32(dataGridView.CurrentCell.RowNumber),0].ToString();
this.dataGridView.Select(dataGridView.CurrentCell.RowNumber); string query_sql = "select * from accounttype where id = '" + txtId.Text + "'";
SqlDataAdapter da = new SqlDataAdapter(query_sql,con);
DataSet ds = new DataSet();
da.Fill(ds,"accounttype");
this.txtTempid.DataBindings.Clear();
this.txtTempid.DataBindings.Add("Text",ds,"AccountType.Id");
}
下面是把获得的唯一字段传给你的From2这时候要注意了,怎么才能把值传过去
private void btnUpdate_Click(object sender, System.EventArgs e)
{
if(txtId.Text != "")
{
//用你Form2的构造函数接收这个值,然后在根据这个值把字段全部绑定到控件上,后来你就可以做更新或者别的什么了,呵呵
FrmUpdate frmupdate = new FrmUpdate(txtId.Text);
this.Close();
str_id = this.txtId.Text;
frmupdate.ShowDialog();
}
else
{
MessageBox.Show("请选择记录进行修改!","提示",MessageBoxButtons.OK,MessageBoxIcon.Question);
}
}
先重新查询数据库,填充到当前DATAGRID中,然后查询要定位的行就可以啦
http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp
这个比较复杂了
如果保存原来的行道是简单,用个变量 保存原来的 位置即可
。
//一下以这种榜定方式为例
dataGrid1.DataSource =ds.Tables[0];
//取得当前绑定上下文的位置
int psoindex = [Form1].BindingContext[ds.Tables[0]].Position
//弹出 Form2 更新数据略.............
//Form2 返回后
[Form1].ds.Tables[0].Clear()
[Form1].da.Fill(ds)
[Form1].BindingContext[ds.Tables[0]].Position = psoindex
/*
建议 方一个 DataView 在窗口上那样 还能取得用户的 排序信息
dv.table= [Form1].ds.Tables[0]
dataGrid1.DataSource = dvint psoindex = [Form1].BindingContext[dv].Position*/不过如果 多用户时 原来选的那行被 其他用户删除了
如和 处理就要你自己考虑了