473,395 Members | 1,418 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Editable Datagrid

I found code on www.codeproject.com. Here is a sample it
is called DataGridColumnDropDown. I modified it to use
the text box when I wanted it to depended on a certain
search criteria.

Public Class ExtendedSearchFieldValueColumnStyle
Inherits DataGridTextBoxColumn
Public WithEvents ColumnComboBox As ComboBox
Public WithEvents ColumnTextBox As TextBox

Private _source As System.Windows.Forms.CurrencyManager
Private _rowNum As Integer
Private _isEditing As Boolean
Public Shared _RowCount As Integer
Public Sub New()
_source = Nothing
_isEditing = False
_RowCount = -1

ColumnComboBox = New NoKeyUpCombo
ColumnComboBox.DropDownStyle =
ComboBoxStyle.DropDown

AddHandler ColumnComboBox.Leave, AddressOf
LeaveComboBox
AddHandler
ColumnComboBox.SelectionChangeCommitted, AddressOf
ComboStartEditing
AddHandler Me.TextBox.Enter, AddressOf
ComboStartEditing
AddHandler Me.TextBox.Leave, AddressOf LeaveTextBox
End Sub 'New

Private Sub HandleScroll(ByVal sender As Object, ByVal
e As EventArgs)
If ColumnComboBox.Visible Then
ColumnComboBox.Hide()
End If
End Sub 'HandleScroll

Private Sub ComboStartEditing(ByVal sender As Object,
ByVal e As EventArgs)
MyBase.ColumnStartedEditing(sender)
End Sub 'ComboMadeCurrent
Private Sub LeaveComboBox(ByVal sender As Object,
ByVal e As EventArgs)
ColumnComboBox.Hide()
AddHandler Me.DataGridTableStyle.DataGrid.Scroll,
New EventHandler(AddressOf HandleScroll)
End Sub 'LeaveComboBox

Private Sub LeaveTextBox(ByVal sender As Object, ByVal
e As EventArgs)
Me.TextBox.Hide()
End Sub

Protected Overloads Overrides Sub Edit(ByVal
currencySource As System.Windows.Forms.CurrencyManager,
ByVal rowNum As Integer, ByVal bounds As
System.Drawing.Rectangle, ByVal isReadOnly As Boolean,
ByVal instantText As String, ByVal cellIsVisible As
Boolean)
Try
MyBase.Edit(currencySource, rowNum, bounds,
isReadOnly, instantText, cellIsVisible)

_rowNum = rowNum
_source = currencySource

' Get to get the
ExtendedSearchData.ExtendedSearch.FieldName value for the
current row
Dim currentRow As DataRowView = CType
(currencySource.Current, DataRowView)
Dim currentValue As Object = currentRow
("RefTrendSearchFieldID")
' If the current value is null then exit
function
If currentValue Is DBNull.Value Then Exit Sub
' otherwise
' Lookup the ExtendedSearchInfo row based on
the value
Dim refTrendSearchFieldID As Integer = CType
(currentValue, Integer)
Dim refTrendSearchField As
ExtendedSearchInfoDataset.RefTrendSearchFieldDataT able
refTrendSearchField =
ExtendedSearchSystem.GetRefTrendSearchFieldByTrend SearchFie
ldID(refTrendSearchFieldID)
' Just in case
If refTrendSearchField.Rows.Count = 0 Then
Exit Sub

Dim refTrendSearchFieldRow As
ExtendedSearchInfoDataset.RefTrendSearchFieldRow
refTrendSearchFieldRow =
refTrendSearchField.Rows(0)

' Show the text box or combobox based on the
ExtendedSearchInfo.DataGridCellType field
Select Case
refTrendSearchFieldRow.sDataGridCellType.Trim()
Case "D"
currentRow("ColumnType") = "D"
ColumnComboBox.Parent =
Me.TextBox.Parent
ColumnComboBox.Location =
Me.TextBox.Location
ColumnComboBox.Size = New Size
(Me.TextBox.Size.Width, ColumnComboBox.Size.Height)
ColumnComboBox.ValueMember
= "lookupValue"
ColumnComboBox.DisplayMember
= "Display"
Dim data As DataTable =
GetCurrentRowFieldValue(currencySource)
ColumnComboBox.DataSource = data
If Not currentRow("DisplayValue") Is
DBNull.Value Then
ColumnComboBox.SelectedIndex =
ColumnComboBox.FindStringExact(CType(currentRow
("DisplayValue"), String))
End If
'ColumnComboBox.Text = Me.TextBox.Text
Me.TextBox.Visible = False
ColumnComboBox.Visible = True
AddHandler
Me.DataGridTableStyle.DataGrid.Scroll, AddressOf
HandleScroll
ColumnComboBox.BringToFront()
ColumnComboBox.Focus()
Case "T"
currentRow("ColumnType") = "T"
Me.ColumnComboBox.Visible = False
Me.TextBox.Visible = True
Me.TextBox.BringToFront()
Me.TextBox.Focus()
End Select
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub 'Edit

Private Function GetCurrentRowFieldValue(ByVal
currencySource As System.Windows.Forms.CurrencyManager) As
ExtendedSearchInfoDataset.ComboResultsDataTable
' Method returns the value of the FieldName column
in the current row
Dim currentRow As DataRowView = CType
(currencySource.Current, DataRowView)
Dim currentValue As Object = currentRow
("RefTrendSearchFieldID")
' If the current value is null then exit function
If currentValue Is DBNull.Value Then Exit Function
' otherwise
' Lookup the ExtendedSearchInfo row based on the
value
Dim refTrendSearchFieldID As Integer = CType
(currentValue, Integer)
Dim refTrendSearchField As
ExtendedSearchInfoDataset.RefTrendSearchFieldDataT able
Dim refTrendSearchFieldRow As
ExtendedSearchInfoDataset.RefTrendSearchFieldRow
refTrendSearchField =
ExtendedSearchSystem.GetRefTrendSearchFieldByTrend SearchFie
ldID(refTrendSearchFieldID)
refTrendSearchFieldRow = refTrendSearchField.Rows
(0)
Dim valueInfo As
ExtendedSearchInfoDataset.ComboResultsDataTable =
ExtendedSearchSystem.GetComboBoxTypes
(refTrendSearchFieldRow.RefTrendSearchFieldID,
refTrendSearchFieldRow.sDropDownSource)
Dim valueInfoRow As
ExtendedSearchInfoDataset.ComboResultsRow

Return valueInfo
End Function
Protected Overrides Function Commit(ByVal dataSource
As System.Windows.Forms.CurrencyManager, ByVal rowNum As
Integer) As Boolean
Try
Dim row As DataRowView = CType
(dataSource.Current, DataRowView)
Dim value As String
If row("ColumnType") Is DBNull.Value Then
Return False

If CType(row("ColumnType"), String) = "D" Then
value = ColumnComboBox.Text
Else
value = TextBox.Text
End If
If value.Length = 0 Then
Return False
Else
SetColumnValueAtRow(dataSource, rowNum,
value)
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Return True
End Function 'Commit

Protected Overrides Function GetColumnValueAtRow(ByVal
currencySource As System.Windows.Forms.CurrencyManager,
ByVal rowNum As Integer) As Object
Try
Dim row As DataRowView = CType
(currencySource.List, DataView)(rowNum)
Return row("DisplayValue")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Function 'GetColumnValueAtRow
Protected Overrides Sub SetColumnValueAtRow(ByVal
currencySource As System.Windows.Forms.CurrencyManager,
ByVal rowNum As Integer, ByVal value As Object)
Try
Dim s As Object = value

Dim row As DataRowView = CType
(currencySource.Current, DataRowView)

row("DisplayValue") = s
If CType(row("ColumnType"), String) = "D"
Then ' Combobox
' Lookup the combobox value by the display
value
Dim dt As DataTable = CType
(Me.ColumnComboBox.DataSource, DataTable)
Dim rows() As DataRow = dt.Select("")
Dim rowCount As Integer = rows.Length
Dim i As Integer = 0
Dim s1 As Object

'if things are slow, you could order your
dataview
'& use binary search instead of this
linear one
While i < rowCount
s1 = rows(i)
(Me.ColumnComboBox.DisplayMember)
If (Not s1 Is DBNull.Value) AndAlso _
s = s1 Then
Exit While
End If
i = i + 1
End While
If i < rowCount Then
s = rows(i)
(Me.ColumnComboBox.ValueMember)
Else
s = DBNull.Value
End If
End If
row("Value") = s
row.EndEdit()

' Call the base method to set the value
If currencySource.Position = rowNum Then
MyBase.SetColumnValueAtRow(currencySource,
rowNum, s)
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub 'SetColumnValueAtRow
End Class
This is an addition to it to help with some errors...

Public Class NoKeyUpCombo
Inherits ComboBox
Private WM_KEYUP As Integer = &H101
Protected Overrides Sub WndProc(ByRef m As
System.Windows.Forms.Message)
If m.Msg = WM_KEYUP Then
'ignore keyup to avoid problem with tabbing &
dropdownlist;
Return
End If
MyBase.WndProc(m)
End Sub 'WndProc
End Class 'NoKeyUpCombo

Maybe this will help you. Look at the web site for more
info on it.
If you drop this code into a .Net class, you can see that
it has Edit functions and Commit functions etc. Just
modify it to meet your needs & standards.

Hope this helps...
-Faith
-----Original Message-----
I have a datagrid that is editable. I have an
<Edititemtemplate> column that is a dropdownlist when in
edit mode. Is there a way to render the a particular
control based on a value within that row. Currently, I
have a datagrid that renders a dropdownlist for a
selected item I want to edit. But there are cases where a
particular row, I just want to display a "label" instead
of a dropdownlist. Is this possible?

Thanks
.

Jul 21 '05 #1
0 4015

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

Similar topics

0
by: Andla Rand | last post by:
Hi, Could you help me to understand how datagrid works. I'm trying to make a datagrid editable. Now most of the tutorials I read says that i should turn the Enable ViewState off. Why is that? ...
1
by: mai via DotNetMonster.com | last post by:
hi, i'm sort of a newbie in .net and i'm currently doing my major project. i would like to know if it is possible to have a datagrid that would not be having a database as its datasource? and is it...
4
by: Stephan Bour | last post by:
Hi, I have a datagrid databound to a SQL query. I'd like to allow editing of some columns but not all. Is there a way to turn off the conversion of the datagrid cells to textboxes for some columns...
2
by: Stephan Bour | last post by:
Hi, Ičve tried to implement a solution to populate a dropdownlist inside an editable datagrid. The code is simple enough and an example can be found here:...
2
by: Jesper Stocholm | last post by:
I have a challenge, that I do not seem to be able to solve. I have an "overview"-page that presently displays data from 2 columns in a database table. The users should be allowed to edit data in...
0
by: DaveR | last post by:
I have a webform with a two-column datagrid based on an Arraylist. The Arraylist draws the data for the two columns from two different tables in an SQL database. The data is displayed in datagrid...
2
by: Carlo Marchesoni | last post by:
I have an editable Datagrid and memorize its datasource with a Session variable. On each Page_Load I simply Bind the Datagrid. Everything works fine. The problem comes, when I have a lot of...
2
by: zambizzi | last post by:
....I can't seem to get my hands on a control I'm loading in an editable datagrid. Here's my datagrid control: <asp:datagrid id="GLRulesGrid" runat="server" autogeneratecolumns="False"...
7
by: Diane | last post by:
Hi- I've been struggling with this problem and none of the fixes posted seem to help out at all. Yet, it seems like such a simple problem... I have a DataGrid, and load it up with data on...
6
by: Richard L Rosenheim | last post by:
I'm sure it can be done, I haven't been able to find the right article yet. I want to be able to filter the records that the datagrid displays, but still have an editable datagrid. That is, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.