Bold Text In Datgrid Row If strResult="Purchased"

J

Jonathan

What code would I need to add below to make the text in datagrid rows bold
when the value of one of the fields, "strResult" equals "Purchased"?

Function bolPopulateDataGrid() as Boolean

Dim oConn As OleDbConnection
Dim oComm As OleDbDataAdapter
Dim sConn As String
Dim sComm As String
Dim oDataSet As New DataSet

'Build the connection string
sConn = "Provider=Microsoft.Jet.OLEDB.4.0;"
sConn += "Data Source=D:\inetpub\www\myweb\fpdb\db.mdb;"
sConn += "Persist Security Info=False"

'Build the SQL string
sComm = "SELECT qryDataGrid01.* "
sComm += "FROM qryDataGrid01;"


'Create the connection and command objects
oConn = New OleDbConnection(sConn)
oComm = New OleDbDataAdapter(sComm, oConn)

'Fill the dataset with the results of the query
oComm.Fill(oDataSet, sComm )

'Set the grid source to the dataset and bind the data
oGrid.DataSource=oDataSet.Tables(sComm).DefaultView
oGrid.DataBind()

End Function
 
J

Jonathan

Thanks but I wanted specific help relating to the VB.net code I listed.

I have been trying to decipher the C# code you enter but haven't yet been
able to get it to work.

Not to also be ungrateful but deciphering the English grammar you wrote I
also find cryptic.


"Steven Cheng" said:
Hi Jonathan,

For DataGrid Row customization, a common approach is using the
"ItemDataBound" or "ItemCreated" event( GridView has "RowDataBound" and
"RowCreated" event). You can get the reference to database column
fields(pass through databinding) and change the certain property on Row (or
individual cells) in it. Here is a simple example that change the row's
Font-bold based on a column value:

======================
protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs
e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{

DataRowView drv = (DataRowView)e.Item.DataItem;

long id = (long)drv[0];

if (id > 3) e.Item.Font.Bold = true;
}
}
=======================

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
What code would I need to add below to make the text in datagrid rows bold
when the value of one of the fields, "strResult" equals "Purchased"?

Function bolPopulateDataGrid() as Boolean

Dim oConn As OleDbConnection
Dim oComm As OleDbDataAdapter
Dim sConn As String
Dim sComm As String
Dim oDataSet As New DataSet

'Build the connection string
sConn = "Provider=Microsoft.Jet.OLEDB.4.0;"
sConn += "Data Source=D:\inetpub\www\myweb\fpdb\db.mdb;"
sConn += "Persist Security Info=False"

'Build the SQL string
sComm = "SELECT qryDataGrid01.* "
sComm += "FROM qryDataGrid01;"


'Create the connection and command objects
oConn = New OleDbConnection(sConn)
oComm = New OleDbDataAdapter(sComm, oConn)

'Fill the dataset with the results of the query
oComm.Fill(oDataSet, sComm )

'Set the grid source to the dataset and bind the data
oGrid.DataSource=oDataSet.Tables(sComm).DefaultView
oGrid.DataBind()

End Function
 
J

Jonathan

My ISP is using .Net 1.1.

I rarely have been programming in asp.net anyway so it hasn't been an issue.

I can't add an event with a click because I am using Frontpage. I haven't
had time to download VS Lite yet.

Do you know what the event code for ItemDataBound would be? I'd have to type
it in directly.
 
S

Steven Cheng

Hi Jonathan,

For DataGrid Row customization, a common approach is using the
"ItemDataBound" or "ItemCreated" event( GridView has "RowDataBound" and
"RowCreated" event). You can get the reference to database column
fields(pass through databinding) and change the certain property on Row (or
individual cells) in it. Here is a simple example that change the row's
Font-bold based on a column value:

======================
protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs
e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{

DataRowView drv = (DataRowView)e.Item.DataItem;

long id = (long)drv[0];

if (id > 3) e.Item.Font.Bold = true;
}
}
=======================

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.



--------------------
 
M

Mark Rae [MVP]

Thanks but I wanted specific help relating to the VB.net code I listed.

I have been trying to decipher the C# code you enter but haven't yet been
able to get it to work.

1) On your DataGrid, add a new ItemDataBound event

2) Drop in the code below:

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
If e.Item.Cells(0).Text = "Purchased" Then ' amend cell number as
necessary
e.Item.Font.Bold = True
End If
End If

N.B. I'm not 100% certain about the above code as I never go anywhere near
VB.NET, but it should be close enough to get you started...

Also, is there any reason that you're using a DataGrid rather than a
GridView? Are you still using ASP.NET v1.1...?
 
M

Mark Rae [MVP]

[top-posting corrected]
My ISP is using .Net 1.1.

Wow! Sounds like it's time to find a proper ISP...
I can't add an event with a click because I am using Frontpage.

Hmm - you could have mentioned that in your original post, this being an
ASP.NET newsgroup...
Do you know what the event code for ItemDataBound would be? I'd have to
type
it in directly.

No idea - what did they tell you in the FrontPage newsgroup...?
 
H

Heinrich Moser

Hi!

1. Go to http://msdn.microsoft.com

2. Enter "DataGrid.ItemDataBound" into the search field in the upper
left-hand corner.

3. Choose the first result.

4. Select the version for ".NET Framework 1.1"

5. Click on "DataGridItemEventHandler".

6. Copy the VB code at the top, replacing "Public Delegate" with
"Protected" and adding "Handles myDataGrid.ItemDataBound" at the
end.

Greetings,
Heinzi
(teaching people to fish)
 
S

Steven Cheng

Hi Jonathan,

Here is the converted VB.NET version of the "ItemDataBound" event handler:

=================
Protected Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then

Dim id As Long
Dim drv As DataRowView = e.Item.DataItem

id = drv(0)

If id > 3 Then
e.Item.Font.Bold = True

End If


End If
End Sub
=================

as others suggested, ASP.NET 2.0 would be much more powerful and simplified
for web application development(especially for some basic data access
tasks).

Steven Cheng
Microsoft MSDN Online Support Lead

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
From: "Jonathan" <[email protected]>
Subject: Re: Bold Text In Datgrid Row If strResult="Purchased"
Date: Mon, 17 Mar 2008 11:43:27 +1200
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top