Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

asp.net gridview head wrecker

  • 18-07-2007 11:12am
    #1
    Closed Accounts Posts: 120 ✭✭


    Hi,

    I have a gridview in asp.net.
    I am looping through the gridview and where there is a particular value in the cell, I change the background color.

    now what I want to do is where the value is 1, or a different color,
    is to enable a hyperlink for those particular cells, not all of them.

    here is my code, hope I make sense

    Dim x As Integer
    Dim y As Integer
    For x = 0 To GridView1.Rows(0).Cells.Count - 1
    If GridView1.Rows(0).Cells(x).Text = 1 Then
    GridView1.Rows(0).Cells(x).BackColor = Drawing.Color.LightBlue
    End If
    For y = 0 To GridView1.Rows.Count - 1
    If GridView1.Rows(y).Cells(x).Text = 1 Then
    GridView1.Rows(y).Cells(x).BackColor = Drawing.Color.LightBlue
    End If

    Next



    Next


Comments

  • Registered Users, Registered Users 2 Posts: 2,931 ✭✭✭Ginger




  • Closed Accounts Posts: 120 ✭✭samelterrance


    Sorry, very new to this ginger.
    I can't see from those articles how I can loops through each cell within the datagrid and how to dynamically make a cell a hyperlink based on it's value.

    i.e 1st row, 1st column, value = 1, hyperlink ="boards.ie"
    2nd row, 1st column, value = 0, no hyperlink


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    What happens with the RowCreate and RowDataBound events is that when the row is created you have access to the cells and the data they are going to contain.

    This means you won't have to loop though the entire GridView reading properties. You should also set the BackColor etc through css (I've commented out the code that does this below). Note the code below is untested so there's probably some minor flaws.

    e.g.
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    	if (e.Row.RowType == DataControlRowType.DataRow)
    	{
    		SomeClassType s = (SomeClassType)e.Row.DataItem;
    
    		if(s.SomeValue == 1)
    		{
    			HyperLink hl = new HyperLink();
                            hl.Text = "Boards.ie";
                            hl.NavigateUrl = "http://www.boards.ie/";
                            e.Row.Cells[0].Controls.Add(hl);
    			//e.Row.Cells[0].CssClass = "SomeCSSClassName";
    			e.Row.Cells[0].BackColor = Drawing.Color.LightBlue;
    		}
    
    	}
    }
    


  • Closed Accounts Posts: 120 ✭✭samelterrance


    Thanks Phil,

    I think it's nearly there !!!!!! just some syntax issues as mine is awful.
    I've converted most to vb.net accept for the first couple of lines below.

    I still cant' see how this will check all cells in the gridview though.


    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
    SomeClassType s = (SomeClassType)e.Row.DataItem;
    If s.SomeValue = 1 Then

    Dim h1 As New HyperLink()
    h1.Text = "boards.ie"
    h1.NavigateUrl = "www.boards.ie"
    e.Row.Cells(0).Controls.Add(h1)
    e.Row.Cells(0).BackColor = Drawing.Color.LightBlue
    End If
    End If

    End Sub


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    If e.Row.RowType = DataControlRowType.DataRow Then
    [b]Dim SomClassType s as SomeClassType
    s = DirectCast(e.Row.DataItem, SomeClassType)[/b]
    If s.SomeValue = 1 Then
    

    I think is going to do it for you. You need to lookup casting in Vb.net.

    What will happen is as each row in the grid view is created you can access that rows Cells collection, and work with each Cell in that Row.


  • Advertisement
  • Closed Accounts Posts: 120 ✭✭samelterrance


    I could kiss you now :-) kidding

    I think I have it though, may not be coded the best but I think it does the job. what you think.


    Dim x As Integer
    x = 0

    If e.Row.RowType = DataControlRowType.DataRow Then
    For x = 0 To e.Row.Cells.Count - 1
    If e.Row.Cells(x).Text = "1" Then
    e.Row.Cells(x).Style.Add("background-color", "red")
    Dim h1 As New HyperLink()
    h1.Text = e.Row.Cells(x).Text
    h1.NavigateUrl = "www.boards.ie"
    e.Row.Cells(x).Controls.Add(h1)
    End If
    Next
    End If


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    Yeah that looks about right. Have a read of http://www.w3schools.com/css/default.asp for more details on CSS but your code is fine.


  • Closed Accounts Posts: 120 ✭✭samelterrance


    Thanks very much for all the help.
    Evil Phil wrote:
    Yeah that looks about right. Have a read of http://www.w3schools.com/css/default.asp for more details on CSS but your code is fine.


  • Registered Users, Registered Users 2 Posts: 2,931 ✭✭✭Ginger


    If you read the MSDN for the base class you will see what the events are used for..

    At least you have it working now. Remember you need to edit most of this stuff before the page is rendered otherwise it wont work


Advertisement