Click here to Skip to main content
15,902,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.


C#
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
       {

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();
}

}
Posted
Updated 4-Oct-11 2:16am
v9
Comments
Bala Selvanayagam 3-Oct-11 7:18am    
Are you saying that you have database table columns (for example) ID,NAME & ADDRESS and then to display only the ID column and when the user clicks on the ID column you want to display NAME & ADDRESS on the same grid same row ?

Handle the DataGridView CellClick event:
C#
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.
 
Share this answer
 
C#
private void grd_CellClick( object sender, DataGridViewCellEventArgs e )
    {
    string val= (string) grd.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
    Console.WriteLine(s);
    }


or you can use

your variable name
suppose string val="";
val=grd.CurrentRow.Cell[int column No.].Value;
 
Share this answer
 
C#
<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.

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900