单击DataGridView行(单元格项),然后在窗口应用程序的C#中显示相应的信息. [英] Click on the DataGridView rows(Cell items) then corresponding info is displayed in C# in window application.

查看:88
本文介绍了单击DataGridView行(单元格项),然后在窗口应用程序的C#中显示相应的信息.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从
获取了三列名为"id","first","last"的datagridview. 数据库.
现在我想要当我单击第一个datagridview行时,然后根据"id"显示相关信息.
同样,当我单击第二个datagridview行时,根据"id"显示相关信息..
同样,当我单击第三个datagridview行时,将显示相关信息.

我正在使用此代码,但有一些小错误.因此无法正常工作.所以请告诉
我正确的方法和代码.

代码是----


I have fetched three columns of datagridview named "id","first","last" from the
database.
Now I want that when I click on the first datagridview row then related info is displayed according to "id".
Similarly when I click on the second datagridview row then related info is displayed according to "id"..
Similarly when I click on the third datagridview row then related info is displayed.

I m using this code but something small error.so it is not working properly.So tell
me the right method and code.

The code is----


private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                string connectionString = ConfigurationSettings.AppSettings["cnn"];
                //int row = e.RowIndex + 1;
                string selectSQL = "SELECT Prefix,first,last FROM contactmngt where id= " + dataGridView1 + "";
                SqlCommand cmd = new SqlCommand(selectSQL, cnn);
                SqlDataReader dr;
                cnn.Open();
                dr = cmd.ExecuteReader();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.SelectCommand = cmd;
                if (dr.Read())
                {
                    label11.Text = dr[0].ToString();
                    label12.Text= dr[1].ToString();
                    label13.Text = dr[2].ToString();
                    
                }
                cnn.Close();
            }
            catch (Exception)
            {
                //Response.Write(ex.ToString());
            }
        }

推荐答案

string selectSQL = "SELECT Prefix,first,last FROM contactmngt where id= " + dataGridView1 + "";

无效.在注释掉之前,您有一个正确的主意(ish):

Is not going to work. You had the right idea (ish) before you commented out:

int row = e.RowIndex + 1;

只需取消"+1"并使用以下信息:

Just take the "+ 1" off and use the information:

string selectSQL = "SELECT Prefix,first,last FROM contactmngt where id=@ID";
SqlCommand cmd = new SqlCommand(selectSQL, cnn);
cmd.Parameters.AddWithValue("@ID", dataGridView1.Rows[e.RowIndex].Cells[0]);


您不需要第二次查询数据库来填充label11,label12和label13而是可以从数据网格读取.

1.希望这是您填充数据gridview的方式

You do not needs to query the database second time for populating label11,label12 and label13 instead can read from the datagrid.

1. Hope this is how you populate the data gridview

try
            {
                using (SqlConnection oConn = new SqlConnection("Data source=localhost;database=Test_db;user=sa;password=admin1990"))
                {
                    oConn.Open();
                    SqlDataAdapter oDa = new SqlDataAdapter("Select Prefix,first,last from contactmngt", oConn);
                    DataSet ods = new DataSet();
                    oDa.Fill(ods);

                    dg.DataSource = ods.Tables[0].DefaultView;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message); 
            }



2.在datagridview"SelectionChanged"事件中,您可以尝试



2. In the datagridview "SelectionChanged" event you can try

private void dg_SelectionChanged(object sender, EventArgs e)
       {
           if (dg.CurrentRow != null)
               lblFirst.Text = dg.CurrentRow.Cells["first"].Value.ToString();
       }





希望这会有所帮助

--Bala-





Hope this helps

--Bala--


这篇关于单击DataGridView行(单元格项),然后在窗口应用程序的C#中显示相应的信息.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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