Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to work with gridview background 1

Status
Not open for further replies.

yosie2007

Technical User
Jun 23, 2007
46
US
I have a gridview with contractid as one column. what I would like to do here is that if the all the first contract id are the same I want to use grey as a background and when the next contid are the same I want white back ground and when the next contract id the same I want to use grey again ......How can I do this. thank you for the help.



Inherits System.Web.UI.Page
Dim prevID As String, currID As String



Protected Sub gvTabulation_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvTabulation.RowDataBound
lblView.Visible = True
lblView.Text = String.Format("You are viewing page {0} of {1} ", gvTabulation.PageIndex + 1, gvTabulation.PageCount)
currID = e.Row.Cells(0).Text

If e.Row.RowType = DataControlRowType.DataRow Then
If prevID <> currID Then
prevID = currID
Dim newColor As String = "white"
e.Row.BackColor = System.Drawing.Color.FromName(newColor)

Else
Dim newColor As String = "Yellow"
e.Row.BackColor = System.Drawing.Color.FromName(newColor)
End If

End If
End Sub
 
use the row type to determine which row your on (Normal, Edit, Select, Alternate). then set the display properties.

if you have multiple lines per contract I would recommend nesting controls within the gridview so each contract is 1 row. you can use repeatable controls in a gridview template (bulleted lists, databound controls, etc.)

I would also recommend not using the Cell(x).Text property. instead set the contract id as the DataKey for the grid. then pull the datakey by row index.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thank you Jason, as you suggested my wish was to use nested gridview and I do not have much experience with that Do you have some simple example on how to work with nested grid view . thanks
 
I got this example from

but the example only works with sqldatasource


If e.Row.RowType = DataControlRowType.DataRow Then
Dim s As SqlDataSource = CType(e.Row.FindControl("SqlDataSource2"), SqlDataSource)
s.SelectParameters(0).DefaultValue = e.Row.Cells(0).Text
End If



this is how I bid to datasource
adTabulation.Fill(dsTabulation, "Tabulation")
gvTabulation.DataSource = dsTabulation
gvTabulation.DataBind()
can I convert the above code to work the way I bind my datasource?
thanks
 
yes, where the data comes from is irrelevant. it's just a data object to populate the datagrid. where they have sqldatasource you have a datatable.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks Jason for the help.
I am not sure I covert it right. can you tell if I make a mistake. thanks


If e.Row.RowType = DataControlRowType.DataRow Then
Dim s As Tabulation = CType(e.Row.FindControl("Tabulation1"), Tabulation)
s.SelectParameters(0).DefaultValue = e.Row.Cells(0).Text
End If
 
if tabulation is your datasource (dataset) then no, it's not correct.

1st I assume your dataset has multiple tables with parent/child relations. if not it should so you can bind child/master data.

you need to define which member of the dataset is bound to the parent grid
Code:
adTabulation.Fill(dsTabulation, "Tabulation")
gvTabulation.DataSource = dsTabulation
gvTabulation.DataMember = "my table name";
gvTabulation.DataBind()
now in your gridview.RowDataBound event you want something like this
Code:
If e.Row.RowType = DataControlRowType.DataRow Then
    Dim data As DataRowView = CType(e.Row.DataItem, DataRowView)
    Dim childCtrl as [bindable collection control] = CType(e.Row.FindControl("my control"), [bindable collection control])

    if childCtrl not nothing then
        childCtrl.DataSource = data.GetChildRows([ds relationship])
        childCtrl.DataBind()
    end if
End If

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thank you so much. I will look at your script carefully and see what I can come up with. thanks
 
Jason sorry if I ask a stupid question but what is bindable collection control? thanks
 
any control that can bind to an enumerated object.
GridView
Repeater
DetailsView
FormView
ListBox
DropDownList
BulletedList
CheckBoxList
etc.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Jason, here is the two tables i have on my ds. what do I substitute this ([ds relationship]) with. thanks hopefully this is the last question :)

adTabulation.Fill(dsTabulation, "Tabulation")
adTabulation.Fill(dsTabulation, "TabDetails")
gvTabulation.DataSource = dsTabulation
gvTabulation.DataMember = "Tabulation"
gvTabulation.DataBind()

data.GetChildRows([ds relationship])
 
have you defined the relationship in the dataset? if not this must be done. if this is a strongly typed dataset, add the relationship in to the xsd.

if the ds is created at runtime then add this line before binding the dataset to the gridview.
Code:
DataColumn parent = ds.Tables("Tabulation").Columns("Id")
DataColumn child= ds.Tables("TabDetails").Columns("TabId")
dsTabulation.Relations.Add("myRelation", parent, child, true)

once this is established pass the relationship name to the function. i didn't check the syntax, so the acutal function may have a different name, or parameters. event so this should point you in the right direction. if the function doens't exist in the DataRowView it should be on the DataRow object (DataRowView.DataRow)
Code:
data.GetChildRows("myRelation")

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks Jason again...I think I am getting there...one more question what if the primary key is a combination keys
what do I do here
Dim Parent As DataColumn = dsTabulation.Tables("TabDetails").Columns("contid")
Dim child As DataColumn = dsTabulation.Tables("TabDetails").Columns("contid")

thanks
 
if you have a composite pk pass an array of columns to the pk property. however the code you provided isn't a compsite key, it's the same column in 2 different variables. so that's still 1 column for the pk.

it would look something like this (my vb is rusty)
Code:
dim pk as DataColumn(2) = new DataColumn(2)
pk(0) = dsTabulation.Tables("TabDetails").Columns("Field 1")
pk(1) = dsTabulation.Tables("TabDetails").Columns("Field 2")
dsTabulation.Tables("TabDetails").PrimaryKey = pk
the same is true for building relationships.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top