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

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

问题描述

当我单击DataGridView1的第一行(第一单元格)时,相应的项目将显示在另一个DataGridView2中.
同样,当我单击DataGridView1的第二行(第一个单元格)时,相应的项目将显示在另一个DataGridView2中. 同样,当我单击DataGridView1的第三行(第一个单元格)时,相应的项目将显示在另一个DataGridView2中.


我的意思是说,当单击窗口应用程序的DataGridView控件的Cell项时(行方向),则应显示其他信息.

我应该使用cell_click方法还是row_click方法.哪种方法合适?

那么它的代码是什么.告诉我.

我正在使用此代码,但无法正常工作.


When I click on first row (first cell) of DataGridView1 then corresponding items is displayed in another DataGridView2.
Similarly When I click on second row (first cell) of DataGridView1 then corresponding items is displayed in another DataGridView2.
Similarly When I click on third row (first cell) of DataGridView1 then corresponding items is displayed in another DataGridView2.


I mean to say that when click on the Cell items(row wise) of the DataGridView control of window application then other information should be displayed.

Should I use cell_click method or row_click method.What method is suitable?

So what is the code for it. Tell me.

I m using this code but it is not working properly.


private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
       {


试试
{

字符串connectionString = ConfigurationSettings.AppSettings ["cnn"];
字符串selectSQL ="SELECT middle,company,title from contactmngt其中id =""+ e.RowIndex +"";
SqlDataAdapter adp =新的SqlDataAdapter(selectSQL,cnn);
SqlCommandBuilder cmd =新的SqlCommandBuilder(adp);
DataTable dtusers =新的DataTable();

//Session ["id1"] = dr ["id"].ToString();

//填写数据表
adp.Fill(dtusers);
DataRow dr = dtusers.Rows [0];
//Panel1.Visible = true;
dataGridView3.DataSource = dtusers;
//调整Datagridview列的大小以使gridview列适合datagridview中的数据
dataGridView3.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);

}
catch(Exception)
{
//MessageBox.Show();
}

}


try
{

string connectionString = ConfigurationSettings.AppSettings["cnn"];
string selectSQL = "SELECT middle,company,title FROM contactmngt where id= ''" + e.RowIndex + "''";
SqlDataAdapter adp = new SqlDataAdapter(selectSQL, cnn);
SqlCommandBuilder cmd = new SqlCommandBuilder(adp);
DataTable dtusers = new DataTable();

//Session["id1"] = dr["id"].ToString();

//Fill data table
adp.Fill(dtusers);
DataRow dr = dtusers.Rows[0];
//Panel1.Visible = true;
dataGridView3.DataSource = dtusers;
//Resize the Datagridview column to fit the gridview columns with data in datagridview
dataGridView3.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);

}
catch (Exception)
{
//MessageBox.Show();
}

}

推荐答案

处理DataGridView CellClick事件:
Handle the DataGridView CellClick event:
private void dataGridView1_CellClick( object sender, DataGridViewCellEventArgs e )
    {
    string s = (string) dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
    Console.WriteLine(s);
    }

然后,您可以根据用户单击的特定单元格显示数据.

You can then display the data based on the specific cell the user as clicked in.


private void grd_CellClick( object sender, DataGridViewCellEventArgs e )
    {
    string val= (string) grd.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
    Console.WriteLine(s);
    }



或者您可以使用

您的变量名
假设字符串val =";
val = grd.CurrentRow.Cell [int列号] .Value;



or you can use

your variable name
suppose string val="";
val=grd.CurrentRow.Cell[int column No.].Value;


<asp:templatefield headertext="Productnumber" itemstyle-horizontalalign="Right" headerstyle-horizontalalign="Left" xmlns:asp="#unknown">
	    <itemtemplate>	 
   <asp:linkbutton id="LNK_Productnumber" runat="server" commandname="Productnumber" cssclass="NoUnderLine " forecolor="Black">
     CommandArgument ='<%# Container.DataItemIndex %>' Text ='<%# Eval("Productnumber") %>'>
                         
	                </asp:linkbutton></itemtemplate>	
</asp:templatefield>





using linkbutton you can find command name and when this command will fire you can perform your operation
use all item as link button. And for remove underline from linkbutton. use CSS.


 <style type="text/css">
                
  .NoUnderLine
    {
  text-decoration: none; 
    }
.highlite
    {
  background-color:Gray;
    }
</style>


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

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