| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2407 人关注过本帖
标题:未将对象引用设置到对象的实例(C#)
只看楼主 加入收藏
czg123698741
Rank: 2
等 级:论坛游民
帖 子:348
专家分:84
注 册:2009-3-4
结帖率:78.99%
收藏
已结贴  问题点数:10 回复次数:3 
未将对象引用设置到对象的实例(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("您选中添加的试题已经存在!");
                }
            }
        }
    }
搜索更多相关主题的帖子: 对象 实例 
2010-02-18 17:10
happy886
Rank: 3Rank: 3
来 自:河南
等 级:论坛游侠
威 望:3
帖 子:130
专家分:120
注 册:2009-3-21
收藏
得分:3 
应该没事啊  我来试试!

happy  C#
2010-02-19 11:15
qubo1982
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:1
帖 子:367
专家分:1132
注 册:2009-3-18
收藏
得分:3 
估计是下标越界了,调试吧,一看就明白了
2010-02-19 16:29
cwbdxxx
该用户已被删除
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽
2010-04-29 16:53
快速回复:未将对象引用设置到对象的实例(C#)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.051792 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved