这是我在百度知道发表的提问
http://zhidao.baidu.com/question/436852705.html?quesup2&oldq=1
想这问题想的头晕,不小心点错了。直接采纳了。 我对自己无语了。 但问题没有解决。
这是Form1.cs中的代码public void setData(string time, string username,string name,string type,string leibie,string money,string moneylast )
        {            
            dataGridView1.Rows[rowIndex].Cells["Column1"].Value = time;
            dataGridView1.Rows[rowIndex].Cells["Column2"].Value = username;
            dataGridView1.Rows[rowIndex].Cells["Column3"].Value = name;
            dataGridView1.Rows[rowIndex].Cells["Column4"].Value = type;
            dataGridView1.Rows[rowIndex].Cells["Column5"].Value = leibie;
            dataGridView1.Rows[rowIndex].Cells["Column6"].Value = money;
            dataGridView1.Rows[rowIndex].Cells["Column7"].Value = moneylast;
            rowIndex++;
        }Form2.cs 中的代码Form2 mf = (Form2)this.Owner;
            mf.setData(System.DateTime.Now.ToString(), textBoxAccount.Text, textBoxName.Text, comboBoxString.Text, type, textBoxInitMoney.Text, textBoxInitMoney.Text);
我的代码不对,但又不知道怎么解决。各位网友帮忙看一下。

解决方案 »

  1.   

    public void AddRow(...)
    {
      datagridview1.Rows.Add();
      int rowIndex = datagridview1.RowCount - 1;
      DataGridViewRow row = datagridview1.Rows[rowIndex];
      row.Cells["Column1"].Value = ...
      ...
    }
      

  2.   

    我错了, 当时我这样想的,因为百度知道插入的有图片,可以更好的解释问题。 在csdn论坛发帖子还没插入过图片。不知道怎么插入图片的。  又添了一些麻烦,真的不好意思。这是问题说明Form1.cs 有一控件如图 图中的dataGidView控件中的列我是手工编辑的。没有绑定数据库。
    Form2.cs的代码中为 为Form1中的dataGirdview控件插入行。
    Form1 和Form2 没有联系,唯一的联系是Form2 中有一button按钮,点击后可以为Form1中dataGridview插入行。此问题如何处理。(涉及到了不同Form间的访问)。
      

  3.   

    public void setData(string time, string username, string name, string type, string leibie, string money, string moneylast)
    {
        int index = this.dataGridView2.Rows.Add();
        dataGridView1.Rows[index].Cells["Column1"].Value = time;
        dataGridView1.Rows[index].Cells["Column2"].Value = username;
        dataGridView1.Rows[index].Cells["Column3"].Value = name;
        dataGridView1.Rows[index].Cells["Column4"].Value = type;
        dataGridView1.Rows[index].Cells["Column5"].Value = leibie;
        dataGridView1.Rows[index].Cells["Column6"].Value = money;
        dataGridView1.Rows[index].Cells["Column7"].Value = moneylast;
    }rowIndex 不用了
      

  4.   

    接6楼,字体样式不能用
    public void setData(string time, string username, string name, string type, string leibie, string money, string moneylast)
    {
        int index = this.dataGridView1.Rows.Add();
        dataGridView1.Rows[index].Cells["Column1"].Value = time;
        dataGridView1.Rows[index].Cells["Column2"].Value = username;
        dataGridView1.Rows[index].Cells["Column3"].Value = name;
        dataGridView1.Rows[index].Cells["Column4"].Value = type;
        dataGridView1.Rows[index].Cells["Column5"].Value = leibie;
        dataGridView1.Rows[index].Cells["Column6"].Value = money;
        dataGridView1.Rows[index].Cells["Column7"].Value = moneylast;
    }