在新的datagridview中显示datagridview选定的行或单元格 [英] Display datagridview selected row or cell in a new datagridview

查看:64
本文介绍了在新的datagridview中显示datagridview选定的行或单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的sql数据库中我有这个表

tblEmployees

EmplName 部门 指定 薪水

John Smith IT研究分析师23000.00

John Michael IT运营经理25000.00

Will Smith IT支持经理13000.00

Lames Brown IT支持经理1000.00

Will支持销售14000.00

Sam Diva IT支持经理助理17000.00



在我的winForms中,我有两个datagridviews(dgv1& dgv2)。在dgv1中显示EmplName列(显示所有名称)。现在我想在第二个datagridview(dgv2)中显示列(Dept,Designation,salary等),如果单击一个单元格/行内容。选择或点击时,这个shd适用于dgv1中的所有EmplNames。(动态



那是我到目前为止所拥有的:





// dgv1 cellcontentclick事件

private void dataGridView1_CellContentClick(object sender,DataGridViewCellEventArgs e)

{

//连接字符串

字符串C =(@数据源= ......);

SqlConnection con = new SqlConnection (C);



// TSQL sp选择表动态
SqlCommand cmd = new SqlCommand();

cmd.Connection = con;

cmd.CommandText =(select * from+ selected cellcontent);



SqlDataAdapter ada = new SqlDataAdapter(cmd);



试试

{

//打开连接

con.Open( );



DataTable dt = new DataTable();

ada.Fill(dt);

/ / dgv2

dataGridView2.AutoGenerateColumns = true;

dataGridView2.DataSource = dt;



}



catch(例外情况)

{

MessageBox.Show(ex.Message);

}

终于

{

con.Close ();

}

In my sql database i have this table
tblEmployees
EmplName Dept Designation salary
John Smith IT Research Research Analyst 23000.00
John Michael IT Operations Manager 25000.00
Will Smith IT Support Manager 13000.00
Lames Brown IT Support Manager 1000.00
Will Emi Support Sales 14000.00
Sam Diva IT Support Manager Assistant 17000.00

In my winForms, i two datagridviews (dgv1 & dgv2). The EmplName column is displayed (showing all names)in dgv1. Now i want to display the columns (Dept, Designation, salary, etc..) in the second datagridview (dgv2) IF a cell / row Content is clicked. And this shd apply for all EmplNames from dgv1 when selected or clicked.(dynamic)

Thatz what i have so far:


//dgv1 cellcontentclick event
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//Connection string
string C = (@"Data Source=…… ");
SqlConnection con = new SqlConnection(C);

// TSQL sp to choose tables dynamic
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = ("select * from " + selected cellcontent"");

SqlDataAdapter ada = new SqlDataAdapter(cmd);

try
{
// Open connection
con.Open();

DataTable dt = new DataTable();
ada.Fill(dt);
//dgv2
dataGridView2.AutoGenerateColumns = true;
dataGridView2.DataSource = dt;

}

catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}

推荐答案

protected void dataGridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    //Accessing BoundField Column
    string name = GridView1.SelectedRow.Cells[0].Text;

    //Accessing TemplateField Column controls
    string country = (GridView1.SelectedRow.FindControl("lblCountry") as Label).Text;

    lblValues.Text = "<b>Name:</b> " + name + " <b>Country:</b> " + country;
}





你能尝试这样吗。事件中有事件部分



can you try like this.event is there is events part in properties


这篇关于在新的datagridview中显示datagridview选定的行或单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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