不同form 同步datagridview

test104 2016-03-01 03:19:56
如何在Form1中,datagridview1输入资料时,同时在Form2-datagridview1显示输入的结果
...全文
218 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
test104 2016-03-02
  • 打赏
  • 举报
回复
如果我一开始进去主画面就打开Form2,之后再打开Form1,怎样让Form1输入资料与Form2的datagridview同步


private void fmMain_Load(object sender, EventArgs e)
        {
Form1 fm = new Form1();
(new Form2(fm.dataGridView1.DataSource)).Show();
}
test104 2016-03-02
  • 打赏
  • 举报
回复
如果我一開始進去主畫面就打開Form2,之後再打開Form1,怎樣把值傳到Form2裡

Fmain fm = new Fmain();
(new Form2(fm.dataGridView1.DataSource)).Show();
正怒月神 2016-03-02
  • 打赏
  • 举报
回复
窗体间传值,构造函数,静态变量,委托都能实现。 网上随便一搜一大把
test104 2016-03-02
  • 打赏
  • 举报
回复
如果使用傳值方式,Form2用Timer還即時更新資料,但Timer在新增一行時會發生



public class Setting
{
public static int colIndex { get; set; }
public static string col1Value { get; set; }
public static string col2Value { get; set; }
}



Form1:

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex != -1)
{
Setting.colIndex = e.RowIndex;
if (dataGridView1.CurrentRow.Cells[0].Value != null)
{
Setting.col1Value = dataGridView1.CurrentRow.Cells[0].Value.ToString();
}
if (dataGridView1.CurrentRow.Cells[1].Value != null)
{
Setting.col2Value = dataGridView1.CurrentRow.Cells[1].Value.ToString();
}
}
}

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex != -1)
{
Setting.colIndex = e.RowIndex;
if (dataGridView1.CurrentRow.Cells[0].Value != null)
{
Setting.col1Value = dataGridView1.CurrentRow.Cells[0].Value.ToString();
}
if (dataGridView1.CurrentRow.Cells[1].Value != null)
{
Setting.col2Value = dataGridView1.CurrentRow.Cells[1].Value.ToString();
}
}
}


Form2

private void timer1_Tick(object sender, EventArgs e)
{

try
{
if (Setting.col1Value != null)
{
dataGridView1.Rows[Setting.colIndex].Cells[0].Value = Setting.col1Value;
}
if (Setting.col2Value != null)
{
dataGridView1.Rows[Setting.colIndex].Cells[1].Value = Setting.col2Value;
}
}
catch
{
Setting.colIndex = this.dataGridView1.Rows.Add();
}

}
qbilbo 2016-03-01
  • 打赏
  • 举报
回复
引用 10 楼 test104 的回复:
如果绑定,我要透過button新增ROW,會錯誤

private void button1_Click(object sender, EventArgs e)
        {        
            dataGridView1.Rows.Add();
            dataGridView1.CurrentRow.Cells[0].Value = "123";
            dataGridView1.CurrentRow.Cells[1].Value = "234";
         }
改成:
((DataTable)dataGridView1.DataSource).Rows.Add(new object[] { "123", "234" });
其实也不是说一定要共用一个Datable来实现。 比如:将Form1中的DataGridView1传给Form2,然后在Form2中对传过来的DataGridView的各类事件进行处理。 如:
public Form2(DataGridView grd)
{
            InitializeComponent();

            grd.NewRowNeeded += ....;
            grd.CellEndEdit += ....;
            .......
}
只是按我对你的需求的理解,共用DataTable是最简单的方法。
test104 2016-03-01
  • 打赏
  • 举报
回复
如果绑定,我要透過button新增ROW,會錯誤

private void button1_Click(object sender, EventArgs e)
        {        
            dataGridView1.Rows.Add();
            dataGridView1.CurrentRow.Cells[0].Value = "123";
            dataGridView1.CurrentRow.Cells[1].Value = "234";
         }
无涯大者 2016-03-01
  • 打赏
  • 举报
回复
可以用委托进行窗体传值来实现!
qbilbo 2016-03-01
  • 打赏
  • 举报
回复
自己做个数据源不就好了。
public partial class Form1 : Form
{
        public Form1()
        {
            InitializeComponent();

            DataTable dt = new DataTable();
            DataColumn[] cols = new DataColumn[2];
            for (int i = 0; i < 2;i++ )
            {
                cols[i] = new DataColumn("Column" + (i + 1).ToString(), typeof(string));
            }
            dt.Columns.AddRange(cols);
            dataGridView1.DataSource = dt;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2(dataGridView1.DataSource);
            frm.Show(this);
        }
}
test104 2016-03-01
  • 打赏
  • 举报
回复
我Form1的dataGridView1沒数据源能绑定,有其他方法嗎?
test104 2016-03-01
  • 打赏
  • 举报
回复
沒反應
qbilbo 2016-03-01
  • 打赏
  • 举报
回复
整条记录更新后就有反应了。
qbilbo 2016-03-01
  • 打赏
  • 举报
回复
对了,Form1的dataGridView1也要绑定数据源才行。
test104 2016-03-01
  • 打赏
  • 举报
回复
qbilbo大大:
沒反應

qbilbo 2016-03-01
  • 打赏
  • 举报
回复
共用一个DataSource就行了。 比如: Form2中
public partial class Form2 : Form
{
        public Form2(object dataSource)
        {
            InitializeComponent();

            dataGridView1.DataSource = dataSource;
        }
}
Form1中:
Form2 frm = new Form2(dataGridView1.DataSource);
frm.Show(this);
xjk3060702235 2016-03-01
  • 打赏
  • 举报
回复
form2加个参数,在form1把资料当参数传过去

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧