使用c#中的dataGridView将文本从一个表单转移到另一个表单?

时间:2016-06-23 08:07:28

标签: c# if-statement text datagridview combobox

我正在尝试使用两个dataGridViews创建一个If语句。在我的应用程序中,我的第二个表单上有一个按钮,我的第一个表单上有两个按钮单元格dataGridViews。应用程序应该像这样运行:用户单击表中的单元格,并将form2中的comboBox中的文本传输到单击的单元格。出于某种原因,我在form2中的if语句中出现错误。

这是我得到的错误:

  

未处理的类型' System.NullReferenceException'发生在Top Shine.exe中   附加信息:对象引用未设置为对象的实例。

这是我的form1代码:

namespace Top_Shine
{
public partial class Top_Shine_Form : Form
{
    public Top_Shine_Form()
    {
        InitializeComponent();
    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
            if (e.ColumnIndex >= 2
              && e.ColumnIndex != 14
              && e.RowIndex != -1
              && dataGridView1.CurrentCell.Value == null)
            {
            var form2 = new Form2();
            form2.tsf1 = this;
            form2.ShowDialog();
        }

        else if (e.ColumnIndex >= 2
               && e.ColumnIndex != 14
               && e.RowIndex != -1
               && dataGridView1.CurrentCell != null
               && dataGridView1.CurrentCell.Value != null
               && dataGridView1.CurrentCell.Value.ToString() != "*")
            {
            var form3 = new Form3();
            form3.f3tsf1 = this;
            form3.ShowDialog();
        }

        else if (e.ColumnIndex >= 2
                && e.ColumnIndex != 14
                && e.RowIndex != -1
                || dataGridView1.CurrentCell.Value.ToString() == "*")
            {
            var form2 = new Form2();
            form2.tsf1 = this;
            form2.ShowDialog();
        }

//////////////////////////////////////////// Color Cells \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

        if (e.ColumnIndex >= 2
           && e.ColumnIndex != 14
           && e.RowIndex != -1
           && dataGridView1.CurrentCell != null
           && dataGridView1.CurrentCell.Value != null
           && dataGridView1.CurrentCell.Value.ToString() != "*"
           && dataGridView1.CurrentCell.Value.ToString().Contains("    X"))
        {
            DataGridViewCellStyle CellStyle = new DataGridViewCellStyle();
            CellStyle.BackColor = Color.GreenYellow;
            dataGridView1.CurrentCell.Style = CellStyle;
        }
        else if (e.ColumnIndex >= 2
           && e.ColumnIndex != 14
           && e.RowIndex != -1
           && dataGridView1.CurrentCell != null
           && dataGridView1.CurrentCell.Value != null
           && dataGridView1.CurrentCell.Value.ToString() != "*")
        {
            DataGridViewCellStyle CellStyle = new DataGridViewCellStyle();
            CellStyle.BackColor = Color.Yellow;
            dataGridView1.CurrentCell.Style = CellStyle;
        }
        dataGridView1.ClearSelection();
    }


..................................................................................................................................................................

    private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex >= 2
          && e.ColumnIndex != 14
          && e.RowIndex != -1
          && dataGridView2.CurrentCell.Value == null)
        {
            var form2 = new Form2();
            form2.tsf2 = this;
            form2.ShowDialog();
        }

        else if (e.ColumnIndex >= 2
               && e.ColumnIndex != 14
               && e.RowIndex != -1
               && dataGridView2.CurrentCell != null
               && dataGridView2.CurrentCell.Value != null
               && dataGridView2.CurrentCell.Value.ToString() != "*")
        {
            var form3 = new Form3();
            form3.f3tsf2 = this;
            form3.ShowDialog();
        }

        else if (e.ColumnIndex >= 2
                && e.ColumnIndex != 14
                && e.RowIndex != -1
                || dataGridView2.CurrentCell.Value.ToString() == "*")
        {
            var form2 = new Form2();
            form2.tsf2 = this;
            form2.ShowDialog();
        }

//////////////////////////////////////////// Color Cells \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

        if (e.ColumnIndex >= 2
           && e.ColumnIndex != 14
           && e.RowIndex != -1
           && dataGridView2.CurrentCell != null
           && dataGridView2.CurrentCell.Value != null
           && dataGridView2.CurrentCell.Value.ToString() != "*"
           && dataGridView2.CurrentCell.Value.ToString().Contains("    X"))
        {
            DataGridViewCellStyle CellStyle = new DataGridViewCellStyle();
            CellStyle.BackColor = Color.GreenYellow;
            dataGridView2.CurrentCell.Style = CellStyle;
        }
        else if (e.ColumnIndex >= 2
           && e.ColumnIndex != 14
           && e.RowIndex != -1
           && dataGridView2.CurrentCell != null
           && dataGridView2.CurrentCell.Value != null
           && dataGridView2.CurrentCell.Value.ToString() != "*")
        {
            DataGridViewCellStyle CellStyle = new DataGridViewCellStyle();
            CellStyle.BackColor = Color.Yellow;
            dataGridView2.CurrentCell.Style = CellStyle;
        }
        dataGridView1.ClearSelection();
}

这是Form2的代码:

        private void button1_Click(object sender, EventArgs e)
    {

        if (tsf1.dataGridView1.CurrentCell.Selected)
        {
            if (interiorComboBox.SelectedItem != null)
            {
                tsf1.dataGridView1.CurrentCell.Value = interiorComboBox.Text;
            }
            if (ExteriorComboBox.SelectedItem != null)
            {
                tsf1.dataGridView1.CurrentCell.Value = ExteriorComboBox.Text;
            }
            if (fitrmvComboBox.SelectedItem != null)
            {
                tsf1.dataGridView1.CurrentCell.Value = fitrmvComboBox.Text;
            }
            if (washComboBox.SelectedItem != null) 
            {
                tsf1.dataGridView1.CurrentCell.Value = washComboBox.Text;
            }
            this.Close();
        }
        else if (tsf2.dataGridView2.CurrentCell.Selected)
        {
            if (interiorComboBox.SelectedItem != null)
            {
                tsf2.dataGridView2.CurrentCell.Value = interiorComboBox.Text;
            }
            if (ExteriorComboBox.SelectedItem != null)
            {
                tsf2.dataGridView2.CurrentCell.Value = ExteriorComboBox.Text;
            }
            if (fitrmvComboBox.SelectedItem != null)
            {
                tsf2.dataGridView2.CurrentCell.Value = fitrmvComboBox.Text;
            }
            if (washComboBox.SelectedItem != null)
            {
                tsf2.dataGridView2.CurrentCell.Value = washComboBox.Text;
            }
        }
        else if(interiorComboBox.SelectedItem == null
                && ExteriorComboBox.SelectedItem == null
                && fitrmvComboBox.SelectedItem == null
                && washComboBox.SelectedItem == null)
        {
            MessageBox.Show("Please Select a Worker");
        }
    }

发生了什么,我该如何解决?

感谢您的帮助。

0 个答案:

没有答案