Free Download

Site Search

 

Top Download

Friday, July 4, 2008

Edit MS Flex Grid in Two Ways

Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

Hi all!

This is my second article on Code Project. This is about how to edit MS Flex Grid. I was using TextBox and ComboBox to edit flex grid at run time, for that I had to add reference of Microsoft.VisualBasic.Compatibility and some complex coding to move TextBox or ComboBox on grid. But this is good idea if some one wants to edit grid with some predefined itemsusing ComboBox.

But if you want to text than there is a simple way to do this; that is the use of Text property of MS Flex Grid Control.

Using the code

To edit MS Flex Grid without using TextBox we have to use .Text property and KeyPressEvent of MS Flex Grid Control.

 
Private Sub AxMSFlexGrid1_KeyPressEvent(ByVal sender As Object, ByVal e As AxMSFlexGridLib.DMSFlexGridEvents_KeyPressEvent) Handles AxMSFlexGrid1.KeyPressEvent

Select Case e.keyAscii

Case 30 To 136

AxMSFlexGrid1.Text += Chr(e.keyAscii)

Case 8

If AxMSFlexGrid1.Text <> "" Then AxMSFlexGrid1.Text = Mid(AxMSFlexGrid1.Text, 1, Len(AxMSFlexGrid1.Text) - 1)

Case 13 And AxMSFlexGrid1.Col <> 2

AxMSFlexGrid1.Col = 2

Case 13 And AxMSFlexGrid1.Col = 2

AxMSFlexGrid1.Rows = AxMSFlexGrid1.Rows + 1

AxMSFlexGrid1.Col = 1

AxMSFlexGrid1.Row = AxMSFlexGrid1.Row + 1

End Select

End Sub

In second techinque I have used a function name moveTextBox to move TextBox at appropriate posiotion and to sent enter text in Grid I have used .Textproperty of Grid control.

 
Private Sub moveTextBox()
TxtEntry.Visible = True
TxtEntry.Left = VB6.TwipsToPixelsX(AxMSFlexGrid2.CellLeft + VB6.TwipsToPixelsX(AxMSFlexGrid2.Left)) + 17
TxtEntry.Top = VB6.TwipsToPixelsY(AxMSFlexGrid2.CellTop + VB6.TwipsToPixelsY(AxMSFlexGrid2.Top)) + AxMSFlexGrid2.Top
TxtEntry.Width = VB6.TwipsToPixelsX(AxMSFlexGrid2.CellWidth)
TxtEntry.Height = VB6.TwipsToPixelsY(AxMSFlexGrid2.CellHeight)
TxtEntry.BringToFront()
TxtEntry.Focus()
End Sub

On TextBox's KeyDown event I have assigned TextBox's .Text property to Flex Grid's .Text property

 
Private Sub TxtEntry_KeyDownEvent(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TxtEntry.KeyDown
If AxMSFlexGrid2.Col = 1 Then
If e.KeyCode = Keys.Enter Then
AxMSFlexGrid2.Text = TxtEntry.Text
TxtEntry.Clear()
AxMSFlexGrid2.Col = 2
End If
End If
End Sub

On EnterCell event of Grid Control I have called this function.


Private Sub AxMSFlexGrid2_EnterCell(ByVal sender As Object, ByVal e As System.EventArgs) Handles AxMSFlexGrid2.EnterCell
If AxMSFlexGrid2.Col = 2 Then
Call moveCombo()
If AxMSFlexGrid2.get_TextMatrix(AxMSFlexGrid2.Row, 2) <> "" Then
CmbEntry.Text = AxMSFlexGrid2.get_TextMatrix(AxMSFlexGrid2.Row, 2).ToString
Else
CmbEntry.Text = ""
End If
Else
CmbEntry.Visible = False
End If
If AxMSFlexGrid2.Col = 1 Then
Call moveTextBox()
If AxMSFlexGrid2.get_TextMatrix(AxMSFlexGrid2.Row, 1) <> "" Then
TxtEntry.Text = AxMSFlexGrid2.get_TextMatrix(AxMSFlexGrid2.Row, 1).ToString
Else
TxtEntry.Clear()
End If
Else
TxtEntry.Visible = False
End If
End Sub

Thanx And Happy Coding..

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

suresh suthar


Hi All.
I am Suresh Suthar from Mumbai India.
I am a Software Developer working in a private firm as a .NET developer.
I born and brought up in a small village Sewari in Rajasthan.
I started my carrier as a data entry operator in 2006. After that I started learning VB 6.0 and VB.NET 2005.
Now I am developing software using VB.Net 2005
and SQL Server 2000/2005.

Here you will some cool source code and a lots of stuff on
SQL Injection, Code Injection, Encription/Decription, Live project, Tutorials on VBScript,JavaScript,Cold Fusion, C#
and many more...

http://www.programmer2programmer.net/
Occupation: Web Developer
Location: India India


http://www.codeproject.com/KB/grid/EditMSFlexGrid.aspx

No comments: