I am trying to make an If statement with two dataGridViews. in my application I have a button on my second form and two button cell dataGridViews on my first form. the application should run like this: the user clicks on a cell from the table and a text from a comboBox in form2 will be transferred to the clicked cell. for some reason I'm getting an error on my if statement in form2.
This is the error that I get:
An unhandled exception of type 'System.NullReferenceException' occurred in Top Shine.exe Additional information: Object reference not set to an instance of an object.
This is my code for 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();
}
And this is the code for 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");
}
}
What is happening and how do I fix it exactly?
Thanks for the help.
Aucun commentaire:
Enregistrer un commentaire