473,769 Members | 6,305 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Editable Datagrid

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

Public Class ExtendedSearchF ieldValueColumn Style
Inherits DataGridTextBox Column
Public WithEvents ColumnComboBox As ComboBox
Public WithEvents ColumnTextBox As TextBox

Private _source As System.Windows. Forms.CurrencyM anager
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.D ropDown

AddHandler ColumnComboBox. Leave, AddressOf
LeaveComboBox
AddHandler
ColumnComboBox. SelectionChange Committed, AddressOf
ComboStartEditi ng
AddHandler Me.TextBox.Ente r, AddressOf
ComboStartEditi ng
AddHandler Me.TextBox.Leav e, AddressOf LeaveTextBox
End Sub 'New

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

Private Sub ComboStartEditi ng(ByVal sender As Object,
ByVal e As EventArgs)
MyBase.ColumnSt artedEditing(se nder)
End Sub 'ComboMadeCurre nt
Private Sub LeaveComboBox(B yVal sender As Object,
ByVal e As EventArgs)
ColumnComboBox. Hide()
AddHandler Me.DataGridTabl eStyle.DataGrid .Scroll,
New EventHandler(Ad dressOf HandleScroll)
End Sub 'LeaveComboBox

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

Protected Overloads Overrides Sub Edit(ByVal
currencySource As System.Windows. Forms.CurrencyM anager,
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(cur rencySource, rowNum, bounds,
isReadOnly, instantText, cellIsVisible)

_rowNum = rowNum
_source = currencySource

' Get to get the
ExtendedSearchD ata.ExtendedSea rch.FieldName value for the
current row
Dim currentRow As DataRowView = CType
(currencySource .Current, DataRowView)
Dim currentValue As Object = currentRow
("RefTrendSearc hFieldID")
' If the current value is null then exit
function
If currentValue Is DBNull.Value Then Exit Sub
' otherwise
' Lookup the ExtendedSearchI nfo row based on
the value
Dim refTrendSearchF ieldID As Integer = CType
(currentValue, Integer)
Dim refTrendSearchF ield As
ExtendedSearchI nfoDataset.RefT rendSearchField DataTable
refTrendSearchF ield =
ExtendedSearchS ystem.GetRefTre ndSearchFieldBy TrendSearchFie
ldID(refTrendSe archFieldID)
' Just in case
If refTrendSearchF ield.Rows.Count = 0 Then
Exit Sub

Dim refTrendSearchF ieldRow As
ExtendedSearchI nfoDataset.RefT rendSearchField Row
refTrendSearchF ieldRow =
refTrendSearchF ield.Rows(0)

' Show the text box or combobox based on the
ExtendedSearchI nfo.DataGridCel lType field
Select Case
refTrendSearchF ieldRow.sDataGr idCellType.Trim ()
Case "D"
currentRow("Col umnType") = "D"
ColumnComboBox. Parent =
Me.TextBox.Pare nt
ColumnComboBox. Location =
Me.TextBox.Loca tion
ColumnComboBox. Size = New Size
(Me.TextBox.Siz e.Width, ColumnComboBox. Size.Height)
ColumnComboBox. ValueMember
= "lookupValu e"
ColumnComboBox. DisplayMember
= "Display"
Dim data As DataTable =
GetCurrentRowFi eldValue(curren cySource)
ColumnComboBox. DataSource = data
If Not currentRow("Dis playValue") Is
DBNull.Value Then
ColumnComboBox. SelectedIndex =
ColumnComboBox. FindStringExact (CType(currentR ow
("DisplayValue" ), String))
End If
'ColumnComboBox .Text = Me.TextBox.Text
Me.TextBox.Visi ble = False
ColumnComboBox. Visible = True
AddHandler
Me.DataGridTabl eStyle.DataGrid .Scroll, AddressOf
HandleScroll
ColumnComboBox. BringToFront()
ColumnComboBox. Focus()
Case "T"
currentRow("Col umnType") = "T"
Me.ColumnComboB ox.Visible = False
Me.TextBox.Visi ble = True
Me.TextBox.Brin gToFront()
Me.TextBox.Focu s()
End Select
Catch ex As Exception
MessageBox.Show (ex.Message)
End Try
End Sub 'Edit

Private Function GetCurrentRowFi eldValue(ByVal
currencySource As System.Windows. Forms.CurrencyM anager) As
ExtendedSearchI nfoDataset.Comb oResultsDataTab le
' 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
("RefTrendSearc hFieldID")
' If the current value is null then exit function
If currentValue Is DBNull.Value Then Exit Function
' otherwise
' Lookup the ExtendedSearchI nfo row based on the
value
Dim refTrendSearchF ieldID As Integer = CType
(currentValue, Integer)
Dim refTrendSearchF ield As
ExtendedSearchI nfoDataset.RefT rendSearchField DataTable
Dim refTrendSearchF ieldRow As
ExtendedSearchI nfoDataset.RefT rendSearchField Row
refTrendSearchF ield =
ExtendedSearchS ystem.GetRefTre ndSearchFieldBy TrendSearchFie
ldID(refTrendSe archFieldID)
refTrendSearchF ieldRow = refTrendSearchF ield.Rows
(0)
Dim valueInfo As
ExtendedSearchI nfoDataset.Comb oResultsDataTab le =
ExtendedSearchS ystem.GetComboB oxTypes
(refTrendSearch FieldRow.RefTre ndSearchFieldID ,
refTrendSearchF ieldRow.sDropDo wnSource)
Dim valueInfoRow As
ExtendedSearchI nfoDataset.Comb oResultsRow

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

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

Protected Overrides Function GetColumnValueA tRow(ByVal
currencySource As System.Windows. Forms.CurrencyM anager,
ByVal rowNum As Integer) As Object
Try
Dim row As DataRowView = CType
(currencySource .List, DataView)(rowNu m)
Return row("DisplayVal ue")
Catch ex As Exception
MessageBox.Show (ex.Message)
End Try
End Function 'GetColumnValue AtRow
Protected Overrides Sub SetColumnValueA tRow(ByVal
currencySource As System.Windows. Forms.CurrencyM anager,
ByVal rowNum As Integer, ByVal value As Object)
Try
Dim s As Object = value

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

row("DisplayVal ue") = s
If CType(row("Colu mnType"), String) = "D"
Then ' Combobox
' Lookup the combobox value by the display
value
Dim dt As DataTable = CType
(Me.ColumnCombo Box.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.ColumnCombo Box.DisplayMemb er)
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.ColumnCombo Box.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.SetColum nValueAtRow(cur rencySource,
rowNum, s)
End If
Catch ex As Exception
MessageBox.Show (ex.Message)
End Try
End Sub 'SetColumnValue AtRow
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
<Edititemtempl ate> 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 4049

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

Similar topics

0
1181
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? If i turn this off then i need to support building the datagrid because the datagrid is not persistant. Right?
1
1379
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 possible to have an editable column in that datagrid wherein it would ask for user input? if so, can anyone be so kind to give me an example or just tell me how to do it? thanks! mai
4
1798
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 when the Edit button is pressed? Thank you, Stephan.
2
1614
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: <http://www.4guysfromrolla.com/webtech/050801-1.shtml> The problem I'm having is that the entire datagrid disappears after I click the Edit link. Could someone tell me what I'm doing wrong? Much appreciated, Stephan.
2
1412
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 the table, and I would think, that I could use an editable datagrid for this. However - I cannot find the right article that tells me how to do it. What I can't seem to figure out is this: The database table-row has 7 fields that should be...
0
1349
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 as expected but when user clicks the Edit link, the cells do NOT turn into editable text boxes. I actually need only the first column to be editable but any ideas on getting the whole row editable would help at first. Apologies for the amount of...
2
1734
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 entries, I mean, more than fits on one page. The user will then scroll down and click on 'edit'. After the roundtrip he has to scroll down again in order to see the row in the editable state. Is there a way to keep the row's position and scroll...
2
5040
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" oneditcommand="GLRulesGrid_Edit"
7
2301
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 page load. In my EditCommand event handler I set the editItemindex. All good. Except, the user needs to click twice to get the DataGrid to become editable. It's obvious to me (perhaps worngly) that ASP.NET is storing the data
6
1614
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 only want records from the table where "State= 'CA'" are to be displayed, but where the user can still edit/add/delete records. The only examples I have found utilizes DataViews, which apparently are not editable. Can someone point me to the...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10045
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...
1
9993
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7406
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...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
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
3
2815
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.