little help with datagrid

B

Brett Sinclair

Hello everybody

I'm still on the learning curve here...and from what I read, I created
inherited datagrid class so I could have icons, combobox...etc in the
columns of my datagrid.

The grid will be used to populate information coming from a Webservice. (No
datasets - No datareaders).
So, I do not know see how to use the "datasource".

Now I'm almost there...but I would need a little bit of help to
programatically populate the datagrid.

Here is , (I hope) a short code that I use. I create a grid with 3 columns.
the column 1 is a column with text + icon called DataGridIconTextColumn
inherited from the class of the same name.
the column 2 is a column with icon called DataGridIconOnlyColumn inherited
from the class of the same name. Depending on some "flag" I will be using 1
icon or another.
The column 3 is a regular DataGridTextBoxColumn


'load some icons from embedded resources
Me.Icons = New ImageList
Dim iconName As String
iconName = "datagridicons.user.ico"
Dim strm As System.IO.Stream
strm = Me.GetType.Assembly.GetManifestResourceStream(iconName)
Dim icon As Icon
icon = New Icon(strm)
Me.Icons.Images.Add(icon.ToBitmap)
iconName = "datagridicons.IconUser1.ico"
strm = Me.GetType.Assembly.GetManifestResourceStream(iconName)
icon = New Icon(strm)
Me.Icons.Images.Add(icon.ToBitmap)
iconName = "datagridicons.IconUser2.ico"
strm = Me.GetType.Assembly.GetManifestResourceStream(iconName)
icon = New Icon(strm)
Me.Icons.Images.Add(icon.ToBitmap)

Dim tableStyle As DataGridTableStyle
tableStyle = New DataGridTableStyle
tableStyle.MappingName = "customers"

Dim numCols As Integer
numCols = 3



Dim iconColumn As DataGridIconTextColumn
iconColumn = New DataGridIconTextColumn(Me.Icons, New
delegateGetIconIndexForRow(AddressOf MyGetImageIndexForRow))
iconColumn.HeaderText = "headercolumn1"
iconColumn.MappingName = "headercolumn2"
tableStyle.GridColumnStyles.Add(iconColumn)

Dim iconColumn2 As DataGridIconOnlyColumn
iconColumn2 = New DataGridIconOnlyColumn(Me.Icons, New
delegateGetIconIndexForRow(AddressOf MyGetImageIndexForRow))
iconColumn2.HeaderText = ""
iconColumn2.MappingName = "Icon"
iconColumn2.Width = Me.Icons.Images(0).Size.Width
tableStyle.GridColumnStyles.Add(iconColumn)

Dim aColumnTextColumn As DataGridTextBoxColumn
aColumnTextColumn = New DataGridTextBoxColumn
aColumnTextColumn.HeaderText = "headercolumn3"
aColumnTextColumn.MappingName = "headercolumn3"
tableStyle.GridColumnStyles.Add(aColumnTextColumn)


' make the dataGrid use our new tablestyle and bind it to our
table
dataGrid1.TableStyles.Clear()
dataGrid1.TableStyles.Add(tableStyle)


Now, using a loop (for instance for i = 0 to 5 ...etc), can somebody show me
how to populate the grid ?
Thank you very much in advance
 
K

Ken Tucker [MVP]

Hi,

Brett what format is the data coming from the webservice in? You
can bind a datagrid to an collection or arraylist.

Ken
--------------------
 
B

Brett Sinclair

Ken,
There will be several "complex" format received.
The most common case will be an array list, which will be composed itself of
columns strings, enum types, date or subarrays.

But for each line, I will display different icons based on the result
received.

Thanks for your help.
 
C

Cor Ligthert

Hi Brett,

The datagrid looks when you go deeper in it almost special made for the
dataset
The webservice is completly optimized around using the XML dataset

Is there a special reason that you do not want to use the dataset?

It is just a container to hold tables.

Cor
 
B

Brett Sinclair

Hi Cor,
yes. There is a reason. The webservice doesn't return a dataset.
It returns arrays of strings, or user type defined , or dates etc. Depending
on which one are called.

Depending on the records received, I need to display some icons on each
line. I also, need to "transform" certain user type defined field, to become
readable to the user.
A simple example is, the webservice send what supposed to be a date field in
the grid as "PS2004-15-01T04:56:42-8:00". So I would need to transform that.

Maybe there is a way to put the results of the web service into a dataset,
then just set the datasource of the datagrid to it ?

Thank you for you help again.
 
C

Cor Ligthert

Hi Brett,

A dataset is so easy to create here a sample I used today.

Maybe you can do something with it.

However if you have more questions feel free to ask.

Cor

\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable
Dim dc As New DataColumn("OHM")
dc.DataType = GetType(System.Int32)
Dim dd As New DataColumn("OHH")
dd.DataType = GetType(System.String)
dt.Columns.Add(dc)
dt.Columns.Add(dd)
For i As Integer = 0 To 11
dt.Rows.Add(dt.NewRow)
dt.Rows(i)(0) = 100
dt.Rows(i)(1) = "1"
Next
Dim ds As New DataSet
ds.Tables.Add(dt)
DataGrid1.DataSource = ds.Tables(0)
End Sub
///
 
B

Brett Sinclair

Hi Cor
I tried that solution. it works. But then I could find how to display an
icon in one the columns based on hte value for each rows.
Based on your example, how would you create a 3rd column that just display
an icon ?

Thank you again for your time.
I really appreciate it.
 
J

Jeff

Here is an example that might help.

In the aspx page:

<asp:DataGrid ID="TheGrid" AutoGenerateColumns=False Runat=server>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Image ImageUrl=<%# GetImageUrl(Container.DataItem) %>
Runat=server></asp:Image>
<%# GetText(Container.DataItem) %>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

In the code behind:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim TheArray As ArrayList = New ArrayList
TheArray.Add(Today())
TheArray.Add("SomeString")
TheArray.Add(23)
TheGrid.DataSource = TheArray
TheGrid.DataBind()
End Sub

Protected Function GetImageUrl(ByVal InDataItem As Object) As String
If TypeOf InDataItem Is Date Then
Return "Date.gif"
ElseIf TypeOf InDataItem Is Integer Then
Return "Integer.gif"
ElseIf TypeOf InDataItem Is String Then
Return "String.gif"
Else
Return "Unknown.gif"
End If
End Function

Protected Function GetText(ByVal InDataItem As Object) As String
If TypeOf InDataItem Is Date Then
Return "Date"
ElseIf TypeOf InDataItem Is Integer Then
Return "Integer"
ElseIf TypeOf InDataItem Is String Then
Return "String"
Else
Return "Unknown"
End If
End Function
 
B

Brett Sinclair

Sorry - my previous answer is not very clear I think....

Hi Cor
I tried that solution at the beginning. It worked fine. But then I could not
find how to display an
icon in one the columns based on hte value for each rows.
That is why, I changed direction, and tried to create an inherited grid,
with an "iconcolumn" type in it. (see code in original post.)

Based on your example, how would you create a 3rd column that just display
an icon ?

Thank you again for your time.
I really appreciate it.
 
B

Brett Sinclair

Hello Jeff
Thank you for your help. I'm not familiar with asp.net, but from what I can
see, is that at design time, you are able to define you column as "Image
ImageUrl".

In vb.net, I think (once again...I'm still learning) that the only type in a
datagrid is a DataGridTextBoxColumn.
I tried to use a datatable, datacolumn and datarow...but I have a same
problem. I can not add an icon to a datacolumn.

That is why I tried to create an inherited datagrid, where a column will
display an icon...but somehow I can not find the syntax now to loop thru the
records to populate that grid once I have defined each columns.

Thank you again for your help.
 
C

Cor Ligthert

Hi Brett,

A datagrid has only two standard columns the textbox column and the bool
column.

I inherited some other controls in the columns myself however never an icon
column and I have now and tomorrow not the time to try to make it for you,
however that should not be a problem for you.

Jan Tielemans a time a regular in this newsgroup has made a complete
datagrid with icons in it and I thought that I had heard it was working
good..

It is free
http://dotnet.leadit.be/extendeddatagrid

Maybe you can try that.

Cor
 
B

Brett Sinclair

Cor
Thank you already for all your help, I will try to look into that grid
provided by Jan Tielemans.

In the meantime, if you inherited some other controls...theoretically it
should work the same.
Let's say on column 3 you have a combobox (already loaded then) or a
label...
how do you build your loop ?

Thank you


\
 

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

Top