已有56人关注
C#从入门到精通的15.6.3的例子调不出来
发表在C#图书答疑 2012-11-28
是否精华
版块置顶:
你好,《C#从入门到精通》第二版的15.6.3 更新数据源部分
我的代码和光盘上的一样,为什么我做的下面几个文本框控件没有内容显示呢?而且在上面正行也不能选中,只能选中某一个单元格,并且在上部选中的时候,下面的文本框控件不显示选中的内容,并且双击单元格的时候,编辑器还会报错,提示“输入的参数有问题”,请问这是为什么?论坛传不了图啊
我的代码如下:
using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Update数据源
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlConnection conn;
        DataSet ds;
        SqlDataAdapter sda;
        private void Form1_Load(object sender, EventArgs e)
        {
            conn = new SqlConnection("server=.;database=db_15;uid=sa;pwd=");
            SqlCommand cmd = new SqlCommand("select * from tb_command", conn);
            sda = new SqlDataAdapter();
            sda.SelectCommand = cmd;
            ds = new DataSet();
            sda.Fill(ds, "cs");
            dataGridView1.DataSource = ds.Tables[0];
        }
        private void button1_Click(object sender, EventArgs e)
        {
            DataTable dt = ds.Tables["cs"];
            sda.FillSchema(dt, SchemaType.Mapped);
            DataRow dr = dt.Rows.Find(txtNo.Text);
            dr["姓名"] = txtName.Text.Trim();
            dr["性别"] = this.txtSex.Text.Trim();
            dr["年龄"] = this.txtAge.Text.Trim();
            dr["奖金"] = this.txtJJ.Text.Trim();
            SqlCommandBuilder cmdbuilder = new SqlCommandBuilder(sda);
            sda.Update(dt);
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtNo.Text = dataGridView1.SelectedCells[0].Value.ToString();
            txtName.Text = dataGridView1.SelectedCells[1].Value.ToString();
            txtSex.Text = dataGridView1.SelectedCells[2].Value.ToString();
            txtAge.Text = dataGridView1.SelectedCells[3].Value.ToString();
            txtJJ.Text = dataGridView1.SelectedCells[4].Value.ToString();

        }
    }
}
分享到:
精彩评论 7
小科_mrkj
学分:43 LV2
2012-11-29
沙发
读者朋友:
    您好,选中dataGridView1控件,在“属性”窗口中切换到事件,看一下CellClick事件是否跟你的相应代码关联上了。
comet6932
学分:0 LV1
TA的每日心情
开心
2020-11-25 22:28:59
2012-11-29
板凳
[FIELDSET][LEGEND]引自:1楼[/LEGEND]
读者朋友:
    您好,选中dataGridView1控件,在“属性”窗口中切换到事件,看一下CellClick事件是否跟你的相应代码关联上了。
[/FIELDSET]

回复:
  
谢谢您的及时回复
的确如您所说,我的dataGridView1_CellClick的事件代码没有加在CellClick事件上,而是加在了CellContentClick上,我已经改过来了,但是问题依旧。[img src=null/img]
dataGridView1控件的属性设置图已提交给客服
小科_mrkj
学分:43 LV2
2012-11-29
地板
读者朋友:
    你好,把dataGridView拖放到窗体中之后,不用其他设置,运行的时候就可以选中行;另外,你现在这种情况不能用SelectedCells,你只选了一个单元格,所以肯定会出现问题,应该以选中行的指定单元格获取数据,比如Rows[0].Cells[i]这种形式。你可以参考一下光盘中的源代码。
comet6932
学分:0 LV1
TA的每日心情
开心
2020-11-25 22:28:59
2012-11-29
4L
txtNo.Text = dataGridView1.SelectedCells[0].Value.ToString();
            txtName.Text = dataGridView1.SelectedCells[1].Value.ToString();
            txtSex.Text = dataGridView1.SelectedCells[2].Value.ToString();
            txtAge.Text = dataGridView1.SelectedCells[3].Value.ToString();
            txtJJ.Text = dataGridView1.SelectedCells[4].Value.ToString();

这个是书上的代码,你说的Rows[0].Cells[i] 我该如何使用呢?
望指教 谢谢
comet6932
学分:0 LV1
TA的每日心情
开心
2020-11-25 22:28:59
2012-11-29
5L

以下是光盘上的示例代码:
using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Test08
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlConnection conn;
        DataSet ds;
        SqlDataAdapter sda;
        private void Form1_Load(object sender, EventArgs e)
        {
            conn = new SqlConnection("server=.;database=db_15;uid=sa;pwd=");
            SqlCommand cmd = new SqlCommand("select * from tb_command", conn);
            sda = new SqlDataAdapter();
            sda.SelectCommand = cmd;
            ds = new DataSet();
            sda.Fill(ds, "cs");
            dataGridView1.DataSource = ds.Tables[0];
        }
        private void button1_Click(object sender, EventArgs e)
        {
            DataTable dt = ds.Tables["cs"];
            sda.FillSchema(dt, SchemaType.Mapped);
            DataRow dr = dt.Rows.Find(txtNo.Text);
            dr["姓名"] = txtName.Text.Trim();
            dr["性别"] = this.txtSex.Text.Trim();
            dr["年龄"] = this.txtAge.Text.Trim();
            dr["奖金"] = this.txtJJ.Text.Trim();
            SqlCommandBuilder cmdbuider = new SqlCommandBuilder(sda);
            sda.Update(dt);
        }
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtNo.Text = dataGridView1.SelectedCells[0].Value.ToString();
            txtName.Text = dataGridView1.SelectedCells[1].Value.ToString();
            txtSex.Text = dataGridView1.SelectedCells[2].Value.ToString();
            txtAge.Text = dataGridView1.SelectedCells[3].Value.ToString();
            txtJJ.Text = dataGridView1.SelectedCells[4].Value.ToString();
        }
    }
}
comet6932
学分:0 LV1
TA的每日心情
开心
2020-11-25 22:28:59
2012-11-29
6L
 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //txtNo.Text = dataGridView1.SelectedCells[0].Value.ToString();
            //txtName.Text = dataGridView1.SelectedCells[1].Value.ToString();
            //txtSex.Text = dataGridView1.SelectedCells[2].Value.ToString();
            //txtAge.Text = dataGridView1.SelectedCells[3].Value.ToString();
            //txtJJ.Text = dataGridView1.SelectedCells[4].Value.ToString();
            txtNo.Text = dataGridView1.Rows[0].Cells[0].Value.ToString();
        }
光盘中的代码,是我上面注释的这几行代码,你说的在下面,可以在文本框控件中显示出值,但是这个值始终是第一个单元格的值,无法变化,也即Rows[0].Cells[0],我怎么能够实现取得当前选中行的对应的列呢?
小科_mrkj
学分:43 LV2
2012-11-29
7L
读者朋友:
    您好,光盘上的dataGridView能选中整行,所有就能用SelectedCells,你这不能选中整行,所以用的话就会出现异常,你重新拖放一个不做任何设置,就可以选中整行。
首页上一页 1 下一页尾页 7 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照