Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am populating my datagridview from my database as following

C#
con.Open();
            SqlCommand cmd = new SqlCommand("select consumerData01.consumerSubDivision as 'Sub-Division',COUNT (*) as TOTAL,"
    + "SUM(case when Office_AppStatus= 'Demand Notice Issued' then 1 else 0 end) as 'Demand Notice Issued',"
    + "SUM(case when Office_AppStatus= 'Survey Report' then 1 else 0 end)as 'Survey',"
    + "SUM(case when Office_AppStatus= 'Rejected' then 1 else 0 end)as 'Rejected',"
    + "SUM(case when Office_AppStatus= 'Submitted' then 1 else 0 end)as 'Submitted',"
    + "SUM(case when Office_AppStatus= 'Meter Installed' then 1 else 0 end)as 'Meter Installed'"
    + "from OfficeDat01 "
    + "inner join consumerData01 on consumerAppRegNo = OfficeDat01.Office_AutoCode "
    + "where consumerData01.consumerRegDate between '" + d1 + "' and '" + d2 + "'"
    + " and consumerSubDivision like '" + s + "%'"
    + "group by consumerSubDivision ", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            if (ds.Tables[0].Rows.Count > 0)
            {
              
                GridView1.DataSource = ds;
                GridView1.DataBind();


            }
            else {

                 GridView1.DataSource = "";
                 GridView1.DataBind();
                 lblMsg.Font.Bold = true;
                 lblMsg.ForeColor = System.Drawing.Color.Red;
                 lblMsg.Text = "No Record Found";


this part is working fine but now I want to show the values as hyper link in datagrid view instead text so that when I click on specific link it should navigate to page showing details of that record.

What I have tried:

I have tried following but it only works for 2nd column only I want to do the same with all columns having numeric value.

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // Get the value in the hyperlink column.
      string HyperLinkValue = e.Row.Cells[1].Text;
      
      HyperLink myLink = new HyperLink();
      myLink.NavigateUrl = HyperLinkValue;
      myLink.Text = HyperLinkValue;
      e.Row.Cells[1].Controls.Add(myLink);
    }
    }
Posted
Updated 7-Feb-17 7:47am
v2

1 solution

Start by fixing the SQL Injection[^] vulnerability in your code:
SqlCommand cmd = new SqlCommand("select consumerData01.consumerSubDivision as 'Sub-Division',COUNT (*) as TOTAL,"
    + "SUM(case when Office_AppStatus= 'Demand Notice Issued' then 1 else 0 end) as 'Demand Notice Issued',"
    + "SUM(case when Office_AppStatus= 'Survey Report' then 1 else 0 end)as 'Survey',"
    + "SUM(case when Office_AppStatus= 'Rejected' then 1 else 0 end)as 'Rejected',"
    + "SUM(case when Office_AppStatus= 'Submitted' then 1 else 0 end)as 'Submitted',"
    + "SUM(case when Office_AppStatus= 'Meter Installed' then 1 else 0 end)as 'Meter Installed'"
    + "from OfficeDat01 "
    + "inner join consumerData01 on consumerAppRegNo = OfficeDat01.Office_AutoCode "
    + "where consumerData01.consumerRegDate between @d1 and @d2"
    + " and consumerSubDivision like @s + '%' "
    + "group by consumerSubDivision ", con);

cmd.Parameters.AddWithValue("@d1", d1);
cmd.Parameters.AddWithValue("@d2", d2);
cmd.Parameters.AddWithValue("@s", s);


Then, change the required columns to hyperlinks. If you're using the GridView control[^], use a HyperLinkField[^]. If you're using the DataGrid control[^], use a HyperLinkColumn[^].

NB: There is no "DataGridView" control in ASP.NET, so it's not clear from your question which control you're using.

Eg:
<asp:HyperLinkField
    HeaderText="Demand Notice Issued"
    DataTextField="Demand Notice Issued"
    DataNavigateUrlFields="Sub-Division"
    DataNavigateUrlFormatString="~/ViewSubDivision.aspx?id={0}"
/>

This will generate a hyperlink with the text set to the value of the "Demand Notice Issued" field, and the URL set to ~/ViewSubDivision.aspx?id=..., putting the value of the "Sub-Division" field after the "id=".


Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]
 
Share this answer
 
Comments
Maciej Los 7-Feb-17 15:05pm    
I really do like links under the line ;)
Aqeel Shoukat 8-Feb-17 1:59am    
Sorry for confusing GridView with DataGridView. I am using GridView and the grid view is being populated at run time. All the numeric values returned from query are converted to hyperlink. Now I want to navigate to new page and pass the row header value of that column and right most column of the hyperlink clicked by user.
Richard Deeming 8-Feb-17 7:48am    
So use a HyperLinkField as I suggested. If you want to pass multiple field values, specify a comma-separated list of field names in the DataNavigateUrlFields property, and use multiple placeholders in the DataNavigateUrlFormatString property.
<asp:HyperLinkField
    ...
    DataNavigateUrlFields="Field0,Field1,Field2"
    DataNavigateUrlFormatString="~/path.aspx?a={0}&b={1}&c={2}"
/>

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