111,120
社区成员
发帖
与我相关
我的任务
分享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.IO;
using System.Collections;
using System.Data .SqlClient ;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private string strCurrentValue;
/// <summary>
/// 用以保存当前单元格的值
/// </summary>
public string StrCurrentValue
{
get { return strCurrentValue; }
set { strCurrentValue = value; }
}
/// <summary>
/// 主窗体构造函数
/// </summary>
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 封装Form1的dataGridView
/// </summary>
public DataGridView Dgv
{
get { return this.dataGridView1; }
}
/// <summary>
/// 为dataGridView添加数据源
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add("id");
table.Columns.Add("name");
DataRow row = table.NewRow();
row["id"] = "007";
row["name"] = "hello world";
table.Rows.Add(row);
DataRow row1 = table.NewRow();
row1["id"] = "008";
row1["name"] = "nihao";
table.Rows.Add(row1);
this.dataGridView1.DataSource = table;
}
/// <summary>
/// 双击单元格内容时发生
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
strCurrentValue = dataGridView1.CurrentCell.Value.ToString ();
int row = this.dataGridView1.CurrentCell.RowIndex;
int col = this.dataGridView1.CurrentCell.ColumnIndex;
Form2 frm = new Form2(strCurrentValue,row ,col,this );
frm.Show();
}
}
}
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;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
private string txtValue;
Form1 frm = null;
int row;
int col;
public Form2(string str,int row,int col,Form1 frm)
{
this.row = row;
this.col = col;
txtValue = str;
InitializeComponent();
this.frm=frm;
}
private void Form2_Load(object sender, EventArgs e)
{
this.textBox1.Text = txtValue;
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
frm.Dgv.Rows[row].Cells[col].Value = this.textBox1.Text;
//若你必须更新数据表,就在这里开始写sql语句。。。
}
}
}
test2 t2=new test2();
t2.ShowDialog();
if (t2.DialogResult == DialogResult.Yes)
{
bind();//重新绑定数据
}
else
{
return;
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace mytest
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
}
private string a = "";
public void seta(string _a)
{
a = _a;
}
private string b = "";
public void setb(string _b)
{
b = _b;
}
private string c = "";
public void setc(string _c)
{
c = _c;
}
private string d = "";
public void setd(string _d)
{
d = _d;
}
private string id = "";
public void setid(string _id)
{
id = _id;
}
private DataTable dt1 = null;
public void setdt(DataTable _dt1)
{
dt1 = _dt1;
}
DataTable dt = new DataTable();
private void button1_Click(object sender, EventArgs e)
{
DataRow dr = dt.NewRow();
dr["a"] = textBox1.Text;
dr["b"] = textBox2.Text;
dr["c"] = textBox3.Text;
dr["d"] = textBox4.Text;
dt.Rows.Add(dr);
dataGridView1.DataSource = dt;
}
private void Form5_Load(object sender, EventArgs e)
{
if (id == "")
{
dt.TableName = "aaa";
dt.Columns.Add("a");
dt.Columns.Add("b");
dt.Columns.Add("c");
dt.Columns.Add("d");
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellCancelEventArgs e)
{
int i = dataGridView1.CurrentRow.Index;
string i1 = i.ToString();
string a1 = dt.Rows[i][0].ToString();
string b1 = dt.Rows[i][1].ToString();
string c1 = dt.Rows[i][2].ToString();
string d1 = dt.Rows[i][3].ToString();
Form6 fr = new Form6();
fr.seta(a1);
fr.setb(b1);
fr.setc(c1);
fr.setd(d1);
fr.setid(i1);
fr.setdt(dt);
this.Visible = false;
fr.ShowDialog();
}
private void Form5_VisibleChanged(object sender, EventArgs e)
{
if (id != "")
{
dt1.Rows[Convert.ToInt32(id)][0] = a;
dt1.Rows[Convert.ToInt32(id)][1] = b;
dt1.Rows[Convert.ToInt32(id)][2] = c;
dt1.Rows[Convert.ToInt32(id)][3] = d;
dataGridView1.DataSource = dt1;
}
//}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace mytest
{
public partial class Form6 : Form
{
public Form6()
{
InitializeComponent();
}
private string a = "";
public void seta(string _a)
{
a = _a;
}
private string b = "";
public void setb(string _b)
{
b = _b;
}
private string c = "";
public void setc(string _c)
{
c = _c;
}
private string d = "";
public void setd(string _d)
{
d = _d;
}
private string id = "";
public void setid(string _id)
{
id = _id;
}
private DataTable dt = null;
public void setdt(DataTable _dt)
{
dt = _dt;
}
private void Form6_Load(object sender, EventArgs e)
{
textBox1.Text = a;
textBox2.Text = b;
textBox3.Text = c;
textBox4.Text = d;
}
private void button1_Click(object sender, EventArgs e)
{
string q1 = textBox1.Text;
string w1 = textBox2.Text;
string e1 = textBox3.Text;
string r1 = textBox4.Text;
string i = id;
Form5 fr = new Form5();
fr.seta(q1);
fr.setb(w1);
fr.setc(e1);
fr.setd(r1);
fr.setid(i);
fr.setdt(dt);
fr.Visible = true;
this.Close();
}
}
}