C# .NET - Detect Datagrid header click
Asked By Aftab Ahmed
03-Feb-12 10:34 PM
I am working on Windows forms app using c# .net 2.0
I am coding on datagrid and written an even on datagrid_clik. This event work perfectlly when click on a row.
My question is, how to detect that a user click is on header of datagrid. because I want that it a user click on row then code should execute and when header is clicked then nothing happened.
Danasegarane Arunachalam replied to Aftab Ahmed
Handle the Datagridview.Cell Click Event
When the user click on the header then the Rowindex will be 0 and when the click on the cell not than the header then rowindex will be > 0
Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
If e.RowIndex = -1 Then
'Click on the Header
Else
'Click not on the header
End If
End Sub
Aftab Ahmed replied to Danasegarane Arunachalam
But due to old version compatibility I am using Data Grid not datagrid view.
so when for example I click on row no 5 then it return the correct row no or index but again when click on header it return the last click row no i:e 5
DATAGRIDVIEW - detect Header click .NET Framework hallo In a DataGridView how / where may I detect the click on the header of a column versus the click on a cell "inside" the grid ? I tried by using the DataGridView1_CellMouseClick event (and other) and by adopting the CurrentCellAddress and have expected a '0' value instead, or something that said to me it was the click on the header row and not on a "good" rorw) Here what I tried: 1) 'get cell indexes
displaying a list box on grid header click i have a grid view populated with database say i have columns name , age, city, country, designation need to display a list box with distinct names when the column header name is clicked, similarly for all columns 1. Set gridview's property AllowSorting = "true" 2 need sorting i just want a list box to be displayed as per the column header click When we click on any column header sort event get fired and this code won't sort your
to bind the Gridview on header click on page load based on button click I have two buttons on the page say button1 and button2 .With both buttons associated is one GridView say Gridview1.On click of button1 gridview1 is populated with data and same with button2.Now if i click on the header of the gridview1, page is postback now on pageload i want to bind the gridview1 in the gridview1. Hi Sachin, Please manage a session / any Global variable. on each button click set some unique value. and while binding grid check sesion / global var and bind the grid accordingly. for eg: on button1 you have
getting values from string array & assiging to the grid header i have a string array country = {"japan", "india", "china", "nepal"} the values in the array list would e dynamic whtever values i get i need o assign it to the grid header caption for (int index = 0; index < count - 1; index++) { foreach (string x incountry) { Response.Write(myitems[index].ToString()); grid.Columns[index].caption = x.ToString(); break; } where count is the column count i am not this- string [] country = { "japan" , "india" , "china" , "nepal" }; for ( int index = 0; index < count - 1; index++) { grid.Columns[index].caption = country[index].ToString(); } Try this and let me know. i had already like to share one example for this First of all you have to take one grid view like this but before that you have to know how many fields comes in multi dimensional Array and declare that in grid view like this. < asp:GridView ID = "GridMulti" runat = "server" AutoGenerateColumns = "false" Font-Names = "Arial" Font