当前位置:编程学习 > C#/ASP.NET >>

怎么点击datagridview中一行数据,并显示在对应的textBox中呢?在下面代码加入哪个调用函数呢?

 //修改挂号类别信息
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                string sqlStr;
                sqlStr = "update ghlb set 号别ID='" + textBox1.Text.Trim() + "',号类='" + textBox2.Text.Trim() + "',科室='" + textBox3.Text.Trim() + "',价格='" + textBox5.Text.Trim() + "',简码='" + textBox6.Text.Trim() + "',急诊标记='" + comboBox1.Text.Trim() + "',说明='" + textBox8.Text.Trim() + "' where 名称='" + textBox4.Text.Trim() + "'";
                SQL.UpdateDB(sqlStr);
                RefreshData();
                ClearAll();

            }
            catch (Exception ex)
            {
                SQL.cn.Close();
                MessageBox.Show(ex.Message);
            }
        } --------------------编程问答-------------------- 你应该在datagridview的CellClick事件,获取改行的数据,然后显示到TextBox中 --------------------编程问答--------------------    //数据控件的RowHeaderMouseClick 事件代码
        private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                int n = dataGridView1.CurrentCell.RowIndex;
                if (n < dataGridView1.RowCount - 1)
                {
                    textBox1.Text = dataGridView1[0, n].Value.ToString().Trim();
                    textBox2.Text = dataGridView1[1, n].Value.ToString().Trim();
                    textBox3.Text = dataGridView1[2, n].Value.ToString().Trim();
                    textBox4.Text = dataGridView1[3, n].Value.ToString().Trim();
                    textBox5.Text = dataGridView1[4, n].Value.ToString().Trim();
                    textBox6.Text = dataGridView1[5, n].Value.ToString().Trim();
                    comboBox1.Text = dataGridView1[6, n].Value.ToString().Trim();
                    textBox8.Text = dataGridView1[7, n].Value.ToString().Trim();

                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
我已经有了这个鼠标点击的调用函数了,欠缺的就是在Form1_Load事件中如何调用那个函数来实现鼠标点击功能
引用楼主  的回复:
 //修改挂号类别信息
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                string sqlStr;
                sqlStr = "update ghlb set 号别ID='……
--------------------编程问答-------------------- 我是这样解决这种问题的:
隐藏button和textbox
前台:
<asp:TextBox ID="TextBox10" runat="server" Width="0px" BorderWidth="0px" 
               Height="0px" BackColor="#EFF7FF"></asp:TextBox>


<asp:Button ID="Button2" runat="server" onclick="Button2_Click" 
               BorderWidth="0px" Height="0px" Width="0px" />

 function ClickEvent1(r1,r3)
         {
           document.getElementById("<%= TextBox10.ClientID %>").value = r1;
           document.getElementById("<%= Button2.ClientID %>").click();
         }




protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", " c =this.style.backgroundColor; this.style.backgroundColor='#96C2F1'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor = c");
            e.Row.Attributes["style"] = "cursor:hand";

            e.Row.Attributes.Add("OnClick", "ClickEvent1('" + e.Row.Cells[0].Text + "','" + e.Row.Cells[2].Text + "')");
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {   
        SqlConnection sqlcon = new SqlConnection();
        sqlcon.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DB_CON"].ConnectionString;
        sqlcon.Open();
        SqlCommand sqlcom = new SqlCommand();
        sqlcom.Connection = sqlcon;
        sqlcom.CommandText = "select * from weather where ID = '" + TextBox10.Text + "' and Timestamp = '" + TextBox12.Text + "'";
        sqlcom.CommandType = CommandType.Text;
        SqlDataAdapter sqlda = new SqlDataAdapter(sqlcom);
        ds = new DataSet();
        sqlda.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            TextBox1.Text = ds.Tables[0].Rows[0][0].ToString();
            TextBox2.Text = ds.Tables[0].Rows[0][8].ToString();
            TextBox3.Text = ds.Tables[0].Rows[0][9].ToString();
            TextBox4.Text = ds.Tables[0].Rows[0][10].ToString();
            TextBox5.Text = ds.Tables[0].Rows[0][3].ToString();
            TextBox6.Text = ds.Tables[0].Rows[0][4].ToString();
            TextBox7.Text = ds.Tables[0].Rows[0][11].ToString();
            TextBox8.Text = ds.Tables[0].Rows[0][12].ToString();
            TextBox9.Text = ds.Tables[0].Rows[0][13].ToString();
        }
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,