Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
XML
<asp:GridView ID="GridView1" runat="server" BackColor="White"
               BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
               ForeColor="Black" GridLines="Vertical">
               <RowStyle BackColor="#F7F7DE" />
               <FooterStyle BackColor="#CCCC99" />
               <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
               <EmptyDataTemplate>
                   No Data Found
               </EmptyDataTemplate>
               <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
               <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
               <AlternatingRowStyle BackColor="White" />
           </asp:GridView>


on code behind I
C#
protected void Page_Load(object sender, EventArgs e)
        {DataSet dt = GetExistingRecords();
                GridView1.DataSource = dt;
                GridView1.DataBind();
        }

private DataSet GetExistingRecords()
        {
            DataSet dt = new DataSet();
            try
            {
                
                OleDbConnection thisConnection = new OleDbConnection(connStr);
                thisConnection.Open();
                string query = "Select Call, EnteredYear, Qrt_Mnth, CreateDt, FilingFile, FilingVolSr, ProjNbr from tbl_DataTracking";
                OleDbCommand command = new OleDbCommand(query, thisConnection);
                var myAdapptor = new OleDbDataAdapter();
                //OleDbCommand command = new OleDbCommand("SELECT * FROM tbl_EStamps", thisConnection);
                myAdapptor.SelectCommand = command;
                myAdapptor.Fill(dt);
                thisConnection.Close();
               
            }
            catch(Exception e)
            {
                Response.Write("Error ->  " + e);
            }
            return dt;
        }<pre />

now here in my grid view i do not have bound fields.
if I add bound fields the grid containsduplicates
i want the first column to be hyperlink.
Please tell me how to do it.
Posted
Updated 3-May-19 3:27am

Try this:
C#
void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var firstCell = e.Row.Cells[0];
        firstCell.Controls.Clear();
        firstCell.Controls.Add(new HyperLink { NavigateUrl = firstCell.Text, Text = firstCell.Text });
    }
}

Be warned that if you bind data to grid only first time page loaded then your changes will disappear.
Found it: here[^]
 
Share this answer
 
Comments
maverick12131 21-Jun-13 4:40am    
Thanks a TON :)
Prasad_Kulkarni 21-Jun-13 4:58am    
Glad it helps!
Thank you for accepting solution.
XML
ou have to make that column as Template Column

<asp:TemplateField HeaderText="">
  <ItemTemplate>
   <asp:HyperLink ID="HyperLink1" runat="server" Text="test" NavigateUrl='<%# Eval("fieldName", "show.aspx?ID={0}") %>'></asp:HyperLink>
  </ItemTemplate>
</asp:TemplateField>
 
Share this answer
 
This worked great. I did have an issue getting the ID of the column to change. So I wrote a procedure to get column id by Name.

In my case I was looking for "Path" as my column name.

C#
protected void gvResults_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var firstCell = e.Row.Cells[GetColumnIndexByName(e.Row,"Path")];
                firstCell.Controls.Clear();
                //firstCell.Controls.Add(new HyperLink { NavigateUrl = firstCell.Text, Text = firstCell.Text, Target = "_blank" });
                firstCell.Controls.Add(new HyperLink { NavigateUrl = firstCell.Text, Text = "Click Here", Target = "_blank" });
            }
        }


private int GetColumnIndexByName(GridViewRow row, string columnName)
        {
            int columnIndex = 0;
            int foundIndex = -1;
            foreach (DataControlFieldCell cell in row.Cells)
            {
                if (cell.ContainingField is BoundField)
                {
                    if (((BoundField)cell.ContainingField).DataField.Equals(columnName))
                    {
                        foundIndex = columnIndex;
                        break;
                    }
                }
                columnIndex++; // keep adding 1 while we don't have the correct name
            }
            return foundIndex;
        }
 
Share this answer
 
Comments
CHill60 3-May-19 10:36am    
Are you balexandre?

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