473,915 Members | 5,813 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with Datagrid

Hi
I have downloaded some code and tried it and nothing happens with the
datagrid. Explain what is wrong and what I have to do please. I have tried
to Import the namespace Hamster and it didn't work. Explain what I have to
do to make that work.

Fia

Imports System
Imports System.Windows. Forms

Imports System.Drawing

Imports System.Data

Imports System.Collecti ons

'Namespace Hamster.Common

'/ Implementation of a ComboBox as a column in a DataGrid

Public Class DataGridComboBo xColumn

Inherits DataGridColumnS tyle

Private _xMargin As Integer = 2

Private _yMargin As Integer = 1

Private _comboBox As ComboBox

Private _oldValue As String = ""

Private _inEdit As Boolean = False

Private _dataTable As DataTable

Private _displayMember, _valueMember As String

'/ <param name="colName"> The name of the column</param>

'/ <param name="dataSourc e">The datasource that contains the lookup
table</param>

'/ <param name="displayMe mber">The member of the lookuptable to
display</param>

'/ <param name="valueMemb er">The member of the lookuptable with the
value</param>

'/ <param name="dataGrid" >The datagrid parent of this column</param>

Public Sub New(ByVal colName As String, ByVal dataSource As DataTable, ByVal
displayMember As String, ByVal valueMember As String, ByVal dataGrid As
DataGrid)

_comboBox = New ComboBox

_comboBox.Visib le = False

_comboBox.DataS ource = dataSource

_dataTable = dataSource

_comboBox.Displ ayMember = displayMember

_displayMember = displayMember

_valueMember = valueMember

_comboBox.Value Member = valueMember

_comboBox.DropD ownStyle = ComboBoxStyle.D ropDownList

Dim _graphicsContex t As Graphics = dataGrid.Create Graphics()

Dim _widest As Single = 0

Dim _stringSize As New SizeF(0, 0)

Dim dr As DataRow

For Each dr In dataSource.Rows

_stringSize = _graphicsContex t.MeasureString (dr(displayMemb er).ToString(),
dataGrid.Font)

If _stringSize.Wid th _widest Then

_widest = _stringSize.Wid th

End If

Next dr

_comboBox.DropD ownWidth = CInt(Math.Ceili ng(_widest))

Me.Width = _comboBox.DropD ownWidth + 25 ' Add the space for the dropdown
arrow

Me.MappingName = colName

Me.HeaderText = colName

dataGrid.Contro ls.Add(_comboBo x)

End Sub 'New

'/ Standard override

'/ <param name="rowNum"></param>

Protected Overrides Sub Abort(ByVal rowNum As Integer)

_inEdit = False

_comboBox.Hide( )

End Sub 'Abort

'/ Standard override

'/ <param name="dataSourc e"></param>

'/ <param name="rowNum"></param>

Protected Overrides Function Commit(ByVal dataSource As CurrencyManager ,
ByVal rowNum As Integer) As Boolean

If Not _inEdit Then

Return True

End If

Try

Dim _value As Object = _comboBox.Selec tedValue

If NullText.Equals (_value) Then

_value = System.Convert. DBNull

End If

Me.SetColumnVal ueAtRow(dataSou rce, rowNum, _value)

Catch

Finally

_inEdit = False

_comboBox.Hide( )

End Try

Return True

End Function 'Commit

'/ Standard override

'/ <param name="source"></param>

'/ <param name="rowNum"></param>

'/ <param name="bounds"></param>

'/ <param name="readOnly" ></param>

'/ <param name="instantTe xt"></param>

'/ <param name="cellIsVis ible"></param>

Protected Overloads Overrides Sub Edit(ByVal [source] As CurrencyManager ,
ByVal rowNum As Integer, ByVal bounds As Rectangle, ByVal [readOnly] As
Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)

_comboBox.Text = ""

Dim _originalBounds As Rectangle = bounds

_oldValue = _comboBox.Text

If Not cellIsVisible Then

Return

End If

bounds.Offset(_ xMargin, _yMargin)

bounds.Width -= _xMargin * 2

bounds.Height -= _yMargin

_comboBox.Bound s = bounds

_comboBox.Visib le = True

'_comboBox.Brin gToFront();

'_comboBox.Focu s();

_comboBox.Selec tedValue = GetText(GetColu mnValueAtRow([source], rowNum))

If Not (instantText Is Nothing) Then

_comboBox.Selec tedValue = instantText

Dim [End] As Integer = _comboBox.Text. Length

_comboBox.Selec t([End], 0)

Else

_comboBox.Selec tAll()

End If

'this.DataGridT ableStyle.DataG rid.Invalidate( OriginalBounds) ;

'_comboBox.Brin gToFront();

_inEdit = True

End Sub 'Edit

'/ Standard override

'/ <returns></returns>

Protected Overrides Function GetMinimumHeigh t() As Integer

Return _comboBox.Prefe rredHeight + _yMargin

End Function 'GetMinimumHeig ht

'/ Standard override

'/ <param name="g"></param>

'/ <param name="val"></param>

Protected Overrides Function GetPreferredHei ght(ByVal g As Graphics, ByVal
val As Object) As Integer

Return FontHeight + _yMargin

End Function 'GetPreferredHe ight

'/ Standard override

'/ <param name="g"></param>

'/ <param name="val"></param>

Protected Overrides Function GetPreferredSiz e(ByVal g As Graphics, ByVal val
As Object) As Size

Dim _extents As Size = Size.Ceiling(g. MeasureString(G etText(val),
Me.DataGridTabl eStyle.DataGrid .Font))

_extents.Width += _xMargin * 2

_extents.Height += _yMargin

Return _extents

End Function 'GetPreferredSi ze

'/ Standard override

'/ <param name="g"></param>

'/ <param name="bounds"></param>

'/ <param name="source"></param>

'/ <param name="rowNum"></param>

Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As
Rectangle, ByVal [source] As CurrencyManager , ByVal rowNum As Integer)

Paint(g, bounds, [source], rowNum, False)

End Sub 'Paint

'/ Standard override

'/ <param name="g"></param>

'/ <param name="bounds"></param>

'/ <param name="source"></param>

'/ <param name="rowNum"></param>

'/ <param name="alignToRi ght"></param>

Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As
Rectangle, ByVal [source] As CurrencyManager , ByVal rowNum As Integer, ByVal
alignToRight As Boolean)

Dim _text As String = GetText(GetColu mnValueAtRow([source], rowNum))

Dim dr As DataRow

For Each dr In _dataTable.Rows

If dr(_valueMember ).ToString() = _text Then

_text = dr(_displayMemb er).ToString()

Exit For

End If

Next dr

PaintText(g, bounds, _text, alignToRight)

End Sub 'Paint

' Helper functions

Private Sub PaintText(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal
[text] As String, ByVal alignToRight As Boolean)

Dim _backBrush = New SolidBrush(Me.D ataGridTableSty le.BackColor)

Dim _foreBrush = New SolidBrush(Me.D ataGridTableSty le.ForeColor)

Dim _rect As Rectangle = bounds

Dim _rectF As RectangleF = RectangleF.op_I mplicit(_rect) 'Konverterar från
Rectangle till RectangleF

Dim _format As New StringFormat

If alignToRight Then

_format.FormatF lags = StringFormatFla gs.DirectionRig htToLeft

End If

Select Case Me.Alignment

Case HorizontalAlign ment.Left

_format.Alignme nt = StringAlignment .Near

Case HorizontalAlign ment.Right

_format.Alignme nt = StringAlignment .Far

Case HorizontalAlign ment.Center

_format.Alignme nt = StringAlignment .Center

End Select

_format.FormatF lags = StringFormatFla gs.NoWrap

g.FillRectangle (_backBrush, _rect)

_rect.Offset(0, _yMargin)

_rect.Height -= _yMargin

g.DrawString([text], Me.DataGridTabl eStyle.DataGrid .Font, _foreBrush,
_rectF, _format)

_format.Dispose ()

End Sub 'PaintText

Private Function GetText(ByVal val As Object) As String

If val Is System.DBNull.V alue Then

Return Me.NullText

End If

If Not (val Is Nothing) Then

Return val.ToString()

Else

Return String.Empty

End If

End Function 'GetText

End Class 'DataGridComboB oxColumn

'End Namespace 'Hamster.Common

-------------------------------------------------------------

'Imports Hamster.Common

Public Class Form1

Inherits System.Windows. Forms.Form

Private Sub Form1_Load(ByVa l sender As Object, ByVal e As System.EventArg s)
Handles MyBase.Load

Dim ts As New DataGridTableSt yle

ts.MappingName = "Columns"

Dim AccessDataTypes As New DataTable

AccessDataTypes .Columns.Add(Ne w DataColumn("Num ber", GetType(Integer )))

AccessDataTypes .Columns.Add(Ne w DataColumn("Nam e", GetType(String) ))

AccessDataTypes .Rows.Add(New Object() {3, "Numeric"})

AccessDataTypes .Rows.Add(New Object() {130, "Text"})

Dim c1 As New DataGridComboBo xColumn("Type", AccessDataTypes , "Name",
"Number", theGrid)

c1.NullText = "3"

ts.GridColumnSt yles.Add(c1)

End Sub

End Class
Jul 4 '06 #1
0 1314

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
5964
by: Junkguy | last post by:
I need some help programmatically causing a row in a DataGrid to "flush" its contents to its bound data (in Visual Studio 6 using Windows Forms with C#). My issue is I want to send an update to a database from a menu command while the user is editing a DataGrid. This is unusual in regard to examples and normal practice in that the cell of the DataGrid still has the focus. In all examples I can find, the user normally presses a button on...
0
1171
by: MrNobody | last post by:
I am desperately in need of some help to get a summary row for my DataGrid. A summary row is just a row always on the bottom which has totals for certain columns. I have been able to find tutorials online that show exactly how to accomplish this using the Web UI DataGrid in ASP.net, but unfortunately the DataGrids are simply not the same- the Windows.Forms DataGrid does not have a footer you can use like the Web UI one has So far the...
1
2226
by: ericm1155 | last post by:
I am trying to display some information from a database in a form that displays one record per line. When the user clicks anywhere on a line, that record is highlighted and selected, and my program will respond to that. The problem is that with a listbox control (which is what it sounds like I need), I can't get the information to format at all. \t-Tabs skew the data unpredicatably - it leaves some words closer together on some lines than...
5
1527
by: Luis E Valencia | last post by:
I need a link on a datagrid, the link must have fields of the database Like this acciones.aspx?iddireccion=1&idindicador=4 Thanks
1
3644
by: Jennyfer J Barco | last post by:
Hello again I have a datagrid and I'm showing many records. I need 2 things: I need that everytime they scroll down, the header stays lock so the user can see it all the time. I need to scroll only the data and keep the header stationary. Is this possible? Second the user has a search bottom and a textbox and when he inserts a value my program selects the row (using MyGrid.SelectedIndex) that matches the value the user entered. If the...
1
1248
by: Anna | last post by:
I have a simple DataGrid that displays a list of characteristics and allows you to edit a description for each characteristic. The query that feeds this involves a join, as the characteristics and their descriptions are in separate tables. The DataKeyField of the DataGrid is set to the description ID, but I also need to be able to access the characteristic ID in my update query (without displaying it in the DataGrid). I haven't had much...
10
1917
by: Terry Olsen | last post by:
I've got a datagrid set up to display data. I've also got an Edit,Update,Cancel column set up to allow editing of data. I've got a DropDownList (ID="ddl3")in the EditItemTemplate for a certain column that I need to populate while in Edit Mode. Two questions: Is there a way to directly use another DropDownList as a DataSource? If so how? I'm using the following code to try to populate the DropDownList, but
21
2397
by: coleenholley | last post by:
I've been trying since last Friday to get an answer on how to get a SPECIFIC row.cell value from a datagrid. I've had plenty of suggestions, but nothing works to get the value from a SPECIFIC Row and cell. Right now I have this code Sub Item_Bound(ByVal sender As Object, ByVal e As DataGridItemEventArgs lbl_test.Text = lbl_test.Text & " " & e.Item.ItemInde Session("wa_tot_gal") = e.Item.Cells(7).Tex Response.Write(Session("wa_tot_gal")...
3
1885
by: Larry Woods | last post by:
I have a datagrid that is carrying all fields of a record...except one. Now I want to update the underlying database via a dataadapter. The update is working but the field that is "left out" is not there, of course. How do I get that field back into the datatable for the database update? TIA, Larry Woods
0
10039
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11354
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10923
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9732
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4778
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3368
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.