472,373 Members | 1,550 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,373 software developers and data experts.

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.Collections

'Namespace Hamster.Common

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

Public Class DataGridComboBoxColumn

Inherits DataGridColumnStyle

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="dataSource">The datasource that contains the lookup
table</param>

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

'/ <param name="valueMember">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.Visible = False

_comboBox.DataSource = dataSource

_dataTable = dataSource

_comboBox.DisplayMember = displayMember

_displayMember = displayMember

_valueMember = valueMember

_comboBox.ValueMember = valueMember

_comboBox.DropDownStyle = ComboBoxStyle.DropDownList

Dim _graphicsContext As Graphics = dataGrid.CreateGraphics()

Dim _widest As Single = 0

Dim _stringSize As New SizeF(0, 0)

Dim dr As DataRow

For Each dr In dataSource.Rows

_stringSize = _graphicsContext.MeasureString(dr(displayMember).T oString(),
dataGrid.Font)

If _stringSize.Width _widest Then

_widest = _stringSize.Width

End If

Next dr

_comboBox.DropDownWidth = CInt(Math.Ceiling(_widest))

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

Me.MappingName = colName

Me.HeaderText = colName

dataGrid.Controls.Add(_comboBox)

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="dataSource"></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.SelectedValue

If NullText.Equals(_value) Then

_value = System.Convert.DBNull

End If

Me.SetColumnValueAtRow(dataSource, 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="instantText"></param>

'/ <param name="cellIsVisible"></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.Bounds = bounds

_comboBox.Visible = True

'_comboBox.BringToFront();

'_comboBox.Focus();

_comboBox.SelectedValue = GetText(GetColumnValueAtRow([source], rowNum))

If Not (instantText Is Nothing) Then

_comboBox.SelectedValue = instantText

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

_comboBox.Select([End], 0)

Else

_comboBox.SelectAll()

End If

'this.DataGridTableStyle.DataGrid.Invalidate(Origi nalBounds);

'_comboBox.BringToFront();

_inEdit = True

End Sub 'Edit

'/ Standard override

'/ <returns></returns>

Protected Overrides Function GetMinimumHeight() As Integer

Return _comboBox.PreferredHeight + _yMargin

End Function 'GetMinimumHeight

'/ Standard override

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

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

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

Return FontHeight + _yMargin

End Function 'GetPreferredHeight

'/ Standard override

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

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

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

Dim _extents As Size = Size.Ceiling(g.MeasureString(GetText(val),
Me.DataGridTableStyle.DataGrid.Font))

_extents.Width += _xMargin * 2

_extents.Height += _yMargin

Return _extents

End Function 'GetPreferredSize

'/ 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="alignToRight"></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(GetColumnValueAtRow([source], rowNum))

Dim dr As DataRow

For Each dr In _dataTable.Rows

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

_text = dr(_displayMember).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.DataGridTableStyle.BackColor)

Dim _foreBrush = New SolidBrush(Me.DataGridTableStyle.ForeColor)

Dim _rect As Rectangle = bounds

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

Dim _format As New StringFormat

If alignToRight Then

_format.FormatFlags = StringFormatFlags.DirectionRightToLeft

End If

Select Case Me.Alignment

Case HorizontalAlignment.Left

_format.Alignment = StringAlignment.Near

Case HorizontalAlignment.Right

_format.Alignment = StringAlignment.Far

Case HorizontalAlignment.Center

_format.Alignment = StringAlignment.Center

End Select

_format.FormatFlags = StringFormatFlags.NoWrap

g.FillRectangle(_backBrush, _rect)

_rect.Offset(0, _yMargin)

_rect.Height -= _yMargin

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

_format.Dispose()

End Sub 'PaintText

Private Function GetText(ByVal val As Object) As String

If val Is System.DBNull.Value 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 'DataGridComboBoxColumn

'End Namespace 'Hamster.Common

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

'Imports Hamster.Common

Public Class Form1

Inherits System.Windows.Forms.Form

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load

Dim ts As New DataGridTableStyle

ts.MappingName = "Columns"

Dim AccessDataTypes As New DataTable

AccessDataTypes.Columns.Add(New DataColumn("Number", GetType(Integer)))

AccessDataTypes.Columns.Add(New DataColumn("Name", GetType(String)))

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

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

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

c1.NullText = "3"

ts.GridColumnStyles.Add(c1)

End Sub

End Class
Jul 4 '06 #1
0 1228

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

Similar topics

11
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...
0
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...
1
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...
5
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
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...
1
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...
10
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...
21
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...
3
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...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

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.