Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I am binding values to GridView from some tables in DB.

The code i used for this is following:

C#
public void gridfill()
{
 try
  {
  reader = obj.SelCommand("Select id as Code,client_name as Client from tbl_client order by cast(id as int)");
  using (reader)
  {
    DataTable dt = new DataTable();
    dt.Load(reader);
    grdCompany.DataSource = dt;
    grdCompany.DataBind();
   }
  catch (Exception ex)
  {
   Response.Write("Error in Page:" + ex.Message);
  }
    }


Now am getting correct result like this:

1 GMA
2 TEC
3 ERT
4 LVV

etc....

Now my question is I WANT TO MAKE THE FIRST CELL "Clickable" and i want to do some coding in the click event of that cell value.

How can i do this?please help me
Posted
Updated 14-Mar-16 19:45pm
v2

This[^] may help you.
 
Share this answer
 
Comments
Obaid Ahmed 24-Feb-11 23:37pm    
ya nice stuff, but in vb..
i think, MR, Rajendran wants in csharp.
may be this will help you more

C#
protected void PeopleGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
            e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.PeopleGridView, "Select$" + e.Row.RowIndex);
        }
    }


you can find more stuff about your question on below 2 links
[Link 1]
[Link 2^]
 
Share this answer
 
this[^] might also help you.
 
Share this answer
 
XML
Hi,
to achieve this you have to use the TemplateField type of column for the first column. Please refer below code example.
01.<%@ Page Language="C#" %>
02.
03.<%@ Import Namespace="System.Data" %>
04.<%@ Import Namespace="System.Data.SqlClient" %>
05.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
06.<script runat="server">
07.
08.  protected void Page_Load(object sender, EventArgs e)
09.  {
10.    if (!IsPostBack)
11.      gridFill();
12.  }
13.
14.  private void gridFill()
15.  {
16.    try {
17.      reader = obj.SelCommand("Select id as Code,client_name as Client from tbl_Client order by cast(id as int)");
18.      using (reader)
19.      {
20.        DataTable dt = new DataTable();
21.        dt.Load(reader);
22.        grdCompany.DataSource = dt;
23.        grdCompany.DataBind();
24.      }
25.    }
26.    catch { }
27.
28.  }
29.
30.  protected void grdCompany_RowCommand(object sender, GridViewCommandEventArgs e)
31.  {
32.    if (e.CommandName.Equals("company"))
33.    {
34.      int companyID = int.Parse(e.CommandArgument.ToString());
35.      //perform your operation using e.CommandArgument
36.    }
37.  }
38.</script>
39.<html xmlns="http://www.w3.org/1999/xhtml">
40.<head runat="server">
41.  <title></title>
42.</head>
43.<body>
44.  <form id="form1" runat="server">
45.  <div>
46.    <asp:GridView ID="grdCompany" runat="server" AutoGenerateColumns="false" OnRowCommand="grdCompany_RowCommand">
47.      <Columns>
48.        <asp:TemplateField HeaderText="ID">
49.          <ItemTemplate>
50.            <asp:LinkButton ID="lbCompanyID" runat="server" CommandArgument='<%#Eval("Code") %>'
51.              CommandName="company"></asp:LinkButton>
52.          </ItemTemplate>
53.        </asp:TemplateField>
54.        <asp:BoundField DataField="Client" HeaderText="Client" />
55.      </Columns>
56.    </asp:GridView>
57.  </div>
58.  </form>
59.</body>
60.</html>
 
Share this answer
 
This might help you out, if no , please feel free to ping for further help.
Making GridView Rows or Individual Cells Clickable and Selectable.
 
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