发布网友 发布时间:2023-04-25 05:41
共2个回答
热心网友 时间:2023-10-14 14:52
在form1的dgv上绑定鼠标右击事件,在事件里面获取当前选中行的数据源中的数据行然后将得到的数据行通过转换或者直接传递给form2,在form2里面定制一个委托用来更新form1中的dgv使用的数据源。追答public partial class Form3 : Form
{
public Form3(){InitializeComponent();}
private void Form3_Load(object sender, EventArgs e){DataTable dt = new DataTable();dt.Columns.Add("proctname", typeof(String));dt.Columns.Add("number", typeof(int));for (var i = 0; i < 30; i++){DataRow row = dt.NewRow();row["proctname"] = "产品" + i;row["number"] = new Random(Guid.NewGuid().GetHashCode()).Next(0, 10000);
dt.Rows.Add(row);
}
dataGridView1.DataSource = dt;
}
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex == -1 || e.ColumnIndex == -1)
{
return;
}
if (e.Button != MouseButtons.Right) return;
DataTable dt = dataGridView1.DataSource as DataTable;
int i = dt.Rows.Count;
DataRow row = dt.Rows[e.RowIndex];
Form4 f = new Form4(row, e.RowIndex);
f.Show();
f.doSaveOperate += new Form4.saveData(f_doSaveOperate);
}
void f_doSaveOperate(DataRow row, int rowIndex)
{
DataTable dt = dataGridView1.DataSource as DataTable;
dt.Rows[rowIndex]["proctname"] = row["proctname"];
dt.Rows[rowIndex]["number"] = row["number"];
dataGridView1.DataSource = dt;
}
}
}
public partial class Form4 : Form
{public delegate void saveData(DataRow row, int rowIndex);
public event saveData doSaveOperate;
DataRow _row;
int _rowIndex;
public Form4(DataRow row, int rowIndex){_row = row;_rowIndex = rowIndex;InitializeComponent(); }
private void Form4_Load(object sender, EventArgs e)
{ textBox1.Text = _row["proctname"].ToString(); textBox2.Text = _row["number"].ToString();}
private void button1_Click(object sender, EventArgs e){_row["proctname"] = textBox1.Text;_row["number"] = textBox2.Text;doSaveOperate(_row, _rowIndex);}
}
热心网友 时间:2023-10-14 14:52
进行编译?如果只是传递数据,可选择传递整个数据源、也可以传递行数据对象