日期:2014-05-17  浏览次数:20850 次

datagridview 数据显示的问题


我在datagridview 中做了一个查询数据的功能 用他来显示出来 但每次查询都会在原来哪一行中增加一行 如图片所示 代码如下
//绑定数据的方法
 private void GetDB(string str)
        {
            //SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=StuManage;Integrated Security=True");
            //con.Open();
            //SqlDataAdapter sdr = new SqlDataAdapter();
            //SqlCommand cmd = new SqlCommand(str, con);
            //sdr.SelectCommand = cmd;
            //DataSet ds = new DataSet();
            con = new SqlConnection("Data Source=.;Initial Catalog=StuManage;Integrated Security=True");
            con.Open();
            sdr = new SqlDataAdapter();
            sdb = new SqlCommandBuilder(sdr);
            cmd = new SqlCommand(str,con);
            sdr.SelectCommand = cmd;
            ds = new DataSet();
            sdr.Fill(ds);
            //向gridview中添加列
            DataGridViewTextBoxColumn col_txb_Id = new DataGridViewTextBoxColumn();
            col_txb_Id.HeaderText = "课程编号";
            col_txb_Id.DataPropertyName = "cno";
            dataGridView1.Columns.Add(col_txb_Id);

            DataGridViewTextBoxColumn col_txb_Name = new DataGridViewTextBoxColumn();
            col_txb_Name.HeaderText = "课程名";
            col_txb_Name.DataPropertyName = "cname";
            dataGridView1.Columns.Add(col_txb_Name);

            DataGridViewTextBoxColumn col_txb_Score = new DataGridViewTextBoxColumn();
            col_txb_Score.HeaderText = "课程学分";
            col_txb_Score.DataPropertyName = "ccredit";
            dataGridView1.Columns.Add(col_txb_Score);

            Da