Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. Determine which cell was clicked in datagrid

Determine which cell was clicked in datagrid

Scheduled Pinned Locked Moved ASP.NET
helptutorialquestioncsharp
3 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Q_Quek
    wrote on last edited by
    #1

    Hi, I got a problem of how to identify the cell was clicked in datagrid. In my datagrid, there are 10 template column and each column have a label and image button. I want to hide the label when user click the image button. I do not know which row of cell is clicked but column. I create a command to the button, for example: HTML: CodeBehind(VB.NET) Public Sub GetControl(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs) Select Case "e.CommandArgument" Case "1" 'Column 1 Dim Label1 as Label = DataGrid1.item(??).FindControl("Label") Label1.visible = False Case "2" 'Column 2 ... End Select End Sub Notice the ??(row), how can I get it??????? I was struggle on this problem couple of day, appreciate for any help! :) Calvin ****

    M 1 Reply Last reply
    0
    • Q Q_Quek

      Hi, I got a problem of how to identify the cell was clicked in datagrid. In my datagrid, there are 10 template column and each column have a label and image button. I want to hide the label when user click the image button. I do not know which row of cell is clicked but column. I create a command to the button, for example: HTML: CodeBehind(VB.NET) Public Sub GetControl(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs) Select Case "e.CommandArgument" Case "1" 'Column 1 Dim Label1 as Label = DataGrid1.item(??).FindControl("Label") Label1.visible = False Case "2" 'Column 2 ... End Select End Sub Notice the ??(row), how can I get it??????? I was struggle on this problem couple of day, appreciate for any help! :) Calvin ****

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Hi there, You can determine the row index thanks to the sender object. Here, the sender is the button that you click:

      Button button = sender as Button;

      then you can specify the row index based on the UniqueID or ClientID properties of the button. In addition, there are a number of different ways to achieve what you need, below are just some of them: + You can get the parent of the button then find the label:

      Button button = sender as Button;
      TableCell cell = button.Parent as TableCell;
      string labelID = "Label" + e.CommandArgument;
      Label label = cell.FindControl(labelID) as Label;
      label.Visible = false;

      + You can get through the child controls of the parent:

      Button button = sender as Button;
      TableCell cell = button.Parent as TableCell;
      foreach(Control ctrl in cell.Controls)
      {
      if(ctrl is Label)
      {
      ctrl.Visible = false;
      return;
      }
      }

      + You can simply create a handler for the ItemCommand event of the DataGrid control. Then in the handler, you can easily determine which Label control should be invisible. The sample code is something like this:

      private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
      {
      //The e.CommandArgument contains the column index.
      string id = "Label" + e.CommandArgument;
      Label label = e.Item.FindControl(id) as Label;
      label.Visible = false;
      }

      Q 1 Reply Last reply
      0
      • M minhpc_bk

        Hi there, You can determine the row index thanks to the sender object. Here, the sender is the button that you click:

        Button button = sender as Button;

        then you can specify the row index based on the UniqueID or ClientID properties of the button. In addition, there are a number of different ways to achieve what you need, below are just some of them: + You can get the parent of the button then find the label:

        Button button = sender as Button;
        TableCell cell = button.Parent as TableCell;
        string labelID = "Label" + e.CommandArgument;
        Label label = cell.FindControl(labelID) as Label;
        label.Visible = false;

        + You can get through the child controls of the parent:

        Button button = sender as Button;
        TableCell cell = button.Parent as TableCell;
        foreach(Control ctrl in cell.Controls)
        {
        if(ctrl is Label)
        {
        ctrl.Visible = false;
        return;
        }
        }

        + You can simply create a handler for the ItemCommand event of the DataGrid control. Then in the handler, you can easily determine which Label control should be invisible. The sample code is something like this:

        private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
        //The e.CommandArgument contains the column index.
        string id = "Label" + e.CommandArgument;
        Label label = e.Item.FindControl(id) as Label;
        label.Visible = false;
        }

        Q Offline
        Q Offline
        Q_Quek
        wrote on last edited by
        #3

        Well, this is good solution, thank you very much Calvin ****

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups