如何使用数据网格视图在表单的文本框中显示数据 [英] how to display data in text boxes of form using data grid view

查看:88
本文介绍了如何使用数据网格视图在表单的文本框中显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个表格,其中有4个标签教师ID,教师姓名,部门和描述以及相应的文本框.我通过ADD按钮(使用插入查询)将值插入数据库中.显示数据.现在的问题是我希望我选择一行datagridview,然后将其显示在相应的文本框中.我尝试了以下代码,但无法正确获得所需的写入.

i have created a form in which there are 4 labels teacher id ,teacher name and department and description along with respective textboxes.i have inserted the values in the database through ADD button (using insert query).I have taken datagrid view to display the data .The problem is now i want that one i select one row of datagridview then it should display in respective text boxes. i have tried following code but not able to get desired write properly.

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=SW-PC-20;Integrated security =SSPI;Initial catalog=institute");
            con.Open();
            SqlCommand com = new SqlCommand("select * from teacher2", con);
            SqlDataReader dr = com.ExecuteReader();
            DataTable dt = new DataTable();

            dt.Load(dr);
            dr.Close();
            dataGridView1.DataSource = dt;

            if (dr.HasRows)

                dr.Read();
            txtteacherid.Text = dr[0].ToString();
            txtteachername.Text = dr[1].ToString();
            txtdepartment.Text = dr[2].ToString();
            txtdescription.Text = dr[3].ToString();
}



请帮忙

问候
shivani



Please help
me
regards
shivani

推荐答案



请在要使用数据获取依据的关键字列中添加查询中的Where子句.


Hi,

Please add the Where Clause in your query with Key Column on which basis you want to fetch data.


private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=SW-PC-20;Integrated security =SSPI;Initial catalog=institute");
            con.Open();
            SqlCommand com = new SqlCommand("select * from teacher2", con);
            SqlDataReader dr = com.ExecuteReader();
            DataTable dt = new DataTable();
 
            dt.Load(dr);
            dataGridView1.DataSource = dt;
            dr.Close();
            con.Close();

}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=SW-PC-20;Integrated security =SSPI;Initial catalog=institute");
            con.Open();
            SqlCommand com = new SqlCommand("select * from Media_Method where Media_method_ID =''" + dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() +"''", con);
            SqlDataReader dr = com.ExecuteReader();
            if (dr.HasRows)
            {
                dr.Read();
                txtteacherid.Text = dr[0].ToString();
                txtteachername.Text = dr[1].ToString();
                txtdepartment.Text = dr[2].ToString();
                txtdescription.Text = dr[3].ToString();
            }
            dr.Close();   
            con.Close();         
}


您也可以直接从gridview直接获取数据,而无需每次都进入数据库来填充文本框.

像这样更改代码,然后尝试是否实现目标?
我想现在您可以理解我要说的内容了吗?

问候
AR


You can directly fetch the data from gridview as well no need to go to database for filling the textboxes every time.

Change your code like this and then try whether your goal achieved or not?
I think now you can understand what I want to say?

regards
AR


使用数组概念将起作用.

ArrayList ar = new ArrayList();
ar.Add(Session [id]);
ar.Add(Session [Name]);
ar.Add(Session [dept]);
ar.Add(Session [desc;]);

grd.DataSource = ar;
grd.DataBind();
txtName.Text = Convert.ToString(Session [Name]);

txtdesc.Text = Convert.ToString(Session [desc]);
txtdept.Text = Convert.ToString(Session [dept]);
Use Array Concept It will Work.

ArrayList ar = new ArrayList();
ar.Add(Session[id]);
ar.Add(Session[ Name]);
ar.Add(Session[dept]);
ar.Add(Session[desc;]);

grd.DataSource = ar;
grd.DataBind();
txtName.Text = Convert.ToString(Session[Name]);

txtdesc.Text = Convert.ToString(Session[desc]);
txtdept.Text = Convert.ToString(Session[dept]);




我注意到您在阅读数据之前先关闭它.并且无需再次将数据绑定到网格视图.

您可以按以下方式修改代码

Hi,

I noticed that you are closing data reader before you read it. And there is no need to bind data to grid view again.

you can modify your code as below

SqlCommand com = new SqlCommand("select * from teacher2", con);
SqlDataReader dr = com.ExecuteReader();
 if (dr.HasRows)
{
while(dr.Read())
{
 txtteacherid.Text = dr[0].ToString();
 txtteachername.Text = dr[1].ToString();
 txtdepartment.Text = dr[2].ToString();
 txtdescription.Text = dr[3].ToString();
}
dr.Close();
}


这篇关于如何使用数据网格视图在表单的文本框中显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆