未将对象引用设置到对象的实例(C#)
大家好,请帮帮小弟的忙啊~~~~~~~~~~~~~代码如下:
public partial class Form1 : Form
{
private DataSet dataSet = new DataSet();
private SqlDataAdapter dataAdapter;
SqlCommand comm;
SqlDataReader dataReader;
string PaperId;
string PaperName;
string GradeName;
string SubjectName;
string Count;
string GenerateTime;
string ii = "";
public Form1()
{
InitializeComponent();
}
private void SelectAll()
{
string sqlSelect = "select PaperId,PaperName,GradeName,SubjectName,Count,GenerateTime from TestPaper";
dataAdapter = new SqlDataAdapter(sqlSelect, DBHelper.conn);
dataAdapter.Fill(dataSet, "TestPaper");
dataGridView1.DataSource = dataSet.Tables["TestPaper"];
}
private void Form1_Load(object sender, EventArgs e)
{
SelectAll();
//设置自增长字段不显示并别改变字段头名字
dataGridView1.Columns[0].Visible = false;
dataGridView1.Columns[1].HeaderText = "试卷名称";
dataGridView1.Columns[2].HeaderText = "适考年级";
dataGridView1.Columns[3].HeaderText = "科目名称";
dataGridView1.Columns[4].HeaderText = "试题数量";
dataGridView1.Columns[5].HeaderText = "生成时间";
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
PaperId = Convert.ToString(dataGridView1.SelectedRows[0].Cells["PaperId"].Value);
PaperName = Convert.ToString(dataGridView1.SelectedRows[0].Cells["PaperName"].Value);
GradeName = Convert.ToString(dataGridView1.SelectedRows[0].Cells["GradeName"].Value);
SubjectName = Convert.ToString(dataGridView1.SelectedRows[0].Cells["SubjectName"].Value);
Count = Convert.ToString(dataGridView1.SelectedRows[0].Cells["Count"].Value);
GenerateTime = Convert.ToString(dataGridView1.SelectedRows[0].Cells["GenerateTime"].Value);
button1.Enabled = true;
//MessageBox.Show(string.Format("{0},{1},{2},{3},{4},{5}", PaperId, PaperName, GradeName, SubjectName, Count, GenerateTime));
}
private void button1_Click(object sender, EventArgs e)
{
string[] row = new string[] {PaperId,PaperName,GradeName,SubjectName,Count,GenerateTime};
object[] rows = new object[] { row };
// 指定DataGridView控件显示的列数
dataGridView2.ColumnCount = row.Length;
dataGridView2.ColumnHeadersVisible = true;
//设置DataGridView控件的标题列名
dataGridView2.Columns[0].Name = "试题编号";
dataGridView2.Columns[0].Visible = false;
dataGridView2.Columns[1].Name = "试卷名称";
dataGridView2.Columns[2].Name = "适考年级";
dataGridView2.Columns[3].Name = "科目名称";
dataGridView2.Columns[4].Name = "试题数量";
dataGridView2.Columns[5].Name = "生成时间";
for (int i = 0; i < 6; i++)
{
ii = this.dataGridView1.Rows[i].Cells[0].Value.ToString(); //未将对象引用设置到对象的实例。(报错!)
MessageBox.Show(ii);
if (ii != PaperId)
{
foreach (string[] rowArray in rows)
{
dataGridView2.Rows.Add(rowArray);
}
}
else
{
MessageBox.Show("您选中添加的试题已经存在!");
}
}
}
}