|
Hello Everyone.
I hope someone can help me with this problem. i have a datatable bound to a bindingsource, and i display its content through a datagridview in this way:
bindingSource1.DataSource = dataTable1
dataGridView1.DataSource = bindingSource1
My problem is that when i update the data contained in the table through the bindingsource, my datagridview does not refresh and keeps displaying the old values. but the strangest thing is that if i manually sort the datagridview by clicking a column header, the datagrid refreshes and displays the updated values. this occurs only if i manually click on the headers, it does not work if i sort the grid in the code or if i call datagridview1.refresh()
I desperately need the datagrid to update its values as i update the bindingsource, without clicking anything. how can i do that?
Thanks in Advance,
Rey9999
~~~ From Milano to The Hague, easy as it goes ~~~
|
|
|
|
|
Have you tried calling Control.Invalidate(), then calling Control.Update() ?
The sequence of these two method calls should force the DataGridView to redraw itself.
|
|
|
|
|
Sebrell wrote: Have you tried calling Control.Invalidate(), then calling Control.Update() ?
The sequence of these two method calls should force the DataGridView to redraw itself.
i tried but i still have the same problem...
~~~ From Milano to The Hague, easy as it goes ~~~
|
|
|
|
|
so much for the simple answer...
I'm reminded of a similar problem I once had with a RichTextBox, where I needed to force it to redraw its scroll-bars. I solved that problem by writing a method along the lines of the following:
(C# code, sorry)
private void Jiggle() {
if (this.Orientation == Orientation.Horizontal) {
this.rtfBox.Width -= 3;
this.rtfBox.Width += 3;
} else {
this.rtfBox.Height -= 3;
this.rtfBox.Height += 3;
}
this.rtfBox.Invalidate();
}
I then called this method from the method that needed to redraw the RichTextBox (which was a property setter for a container control). I was surprised how well it worked. Especially since the RichTextBox in question had its Dock property set to DockStyle.Fill.
I'd suggest dynamically changing, by a small number of pixels (fewer than 8), the Width of one of the ColumnHeaders in your DataGridView, since it works when you click it. Or to change the Sort column to a different column, then back to the original column.
HTH
Sebrell
-- modified at 14:30 Wednesday 18th October, 2006
|
|
|
|
|
i'll try, thanks
~~~ From Milano to The Hague, easy as it goes ~~~
|
|
|
|
|
I found the solution by chance... i realized i was updating the bindingsource's datasource instead of the bindingsource itself. may i be cursed eternally for my mistake
~~~ From Milano to The Hague, easy as it goes ~~~
|
|
|
|
|
I have down loaded the following code form Microsoft site and it is working.
But I am unable to understand how I will trap it in my keyup or keypress event or any other way.
Can any body help me please.
With regards
Suman
How to trap keystrokes in .NET controls by using Visual Basic .NET or Visual Basic 2005
View products that this article applies to.
On This Page
SUMMARY
Set Up the Key Trap
Implement the Overridden Method
Build an Example
SUMMARY
This step-by-step article demonstrates how to trap keystrokes in Windows Forms controls. By using the sample code in this article, you can intercept almost any individual keystroke. You also can intercept key combinations, including CTRL and ALT. The Print Screen key is not affected by this technique. Additionally, some keystrokes from keyboards with additional keys, such as keys that control a Web browser or a CD-ROM player, might not be captured.
For most purposes, the standard KeyUp, KeyDown, and KeyPress events are enough to capture and handle keystrokes. However, not all controls raise these events for all keystrokes under all conditions.
For example, consider the DataGrid control: If no data has been assigned to the grid, the arrow keys (LEFT ARROW, RIGHT ARROW, UP ARROW, and DOWN ARROW) raise only the KeyUp event. Other keys, such as A or 4, raise all three events. If the DataGrid is currently displaying data, none of the standard keyboard events are raised for the navigation keys. Keystrokes such as A or 4 raise no events, raise only KeyUp, or raise all three events, depending on what is currently selected in the control. In these situations, you can follow the steps in this article to capture keystrokes, regardless of the state of the control.
The code samples in this article are written to work with the DataGrid, because this is the control for which this feature is most frequently requested. You can use this same approach with other .NET controls.
Set Up the Key Trap
To trap keystrokes in a Windows Forms control, you derive a new class that is based on the class of the control that you want. You override the ProcessCmdKey method. In this overridden method, you will place the code to process the keystrokes that you want to trap. The following sample code is an example of the basic structure for such a class:
Class MyDataGrid
Inherits DataGrid
Protected Overrides Function ProcessCmdKey( ByRef msg As Message, ByVal keyData As Keys ) As Boolean
End Function
End Class
Implement the Overridden Method
The system passes two parameters to the ProcessCmdKey method: msg and keyData. The msg parameter contains the Windows Message, such as WM_KEYDOWN. The keyData parameter contains the key code of the key that was pressed. If CTRL or ALT was also pressed, the keyData parameter contains the ModifierKey information.
Using the msg parameter is not mandatory; you can ignore it. It is good practice, however, to test the message. In this example, you test WM_KEYDOWN to verify that this is a keystroke event. You also test WM_SYSKEYDOWN, so that it is possible to catch keystroke combinations that involve control keys (primarily ALT and CTRL).
To trap specific keys, you can evaluate the keyCode by comparing it to the Keys enumeration. The following code sample demonstrates how to catch the keystrokes UP ARROW, DOWN ARROW, TAB, CTRL+M, and ALT+Z:
Const WM_KEYDOWN As Integer = &H100
Const WM_SYSKEYDOWN As Integer = &H104
If ((msg.Msg = WM_KEYDOWN) Or (msg.Msg = WM_SYSKEYDOWN)) Then
Select Case (keyData)
Case Keys.Down Console.WriteLine("Down Arrow Captured")
Case Keys.Up Console.WriteLine("Up Arrow Captured")
Case Keys.Tab Console.WriteLine("Tab Key Captured")
Case (Keys.Control Or Keys.M) Console.WriteLine("<ctrl> + m Captured")
Case (Keys.Alt Or Keys.Z) Console.WriteLine("<alt> + z Captured")
End Select
End If
Build an Example
The following example shows how to trap keystrokes with the DataGrid control.
1.Create a new Windows Control Library project in Visual Basic .NET or in Visual Basic 2005.
2.View the properties for the class UserControl1, and then change the name to MyDataGrid.
3.View the code for the Control Library, and then change the following line of code
Inherits System.Windows.Forms.UserControl
to the following:
Inherits System.Windows.Forms.DataGrid
4.Add the following method to the MyDataGrid class:
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, _ ByVal keyData As Keys) As Boolean
Const WM_KEYDOWN As Integer = &H100
Const WM_SYSKEYDOWN As Integer = &H104
If ((msg.Msg = WM_KEYDOWN) Or (msg.Msg = WM_SYSKEYDOWN)) Then
Select Case (keyData)
Case Keys.Down Me.Parent.Text = "Down Arrow Captured"
Case Keys.Up Me.Parent.Text = "Up Arrow Captured"
Case Keys.Tab Me.Parent.Text = "Tab Key Captured"
Case (Keys.Control Or Keys.M) Me.Parent.Text = "<ctrl> + M Captured"
Case (Keys.Alt Or Keys.Z) Me.Parent.Text = "<alt> + Z Captured"
End Select
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
5.Build the project.
6.Create a new Windows Application project in Visual Basic .NET or in Visual Basic 2005. By default, Form1 is created.
Note You must change the code in Visual Basic 2005. By default, Visual Basic creates two files for the project when you create a Windows Forms project. If the form is named Form1, the two files that represent the form are named Form1.vb and Form1.Designer.vb. You write the code in the Form1.vb file. The Windows Forms Designer writes the code in the Form1.Designer.vb file. The Windows Forms Designer uses the partial keyword to divide the implementation of Form1 into two separate files. This behavior prevents the designer-generated code from being interspersed with your code.
For more information about the new Visual Basic 2005 language enhancements, visit the following Microsoft Developer Network (MSDN) Web site:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnhcvs04/html/vs04k1.asp
For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/ms171843.aspx7. On the Tools menu, click Customize Toolbox.
8.Click the .NET Framework Components tab.
9.Click Browse, find the control/DLL that was just created, and then click OK.
10.The control MyDataGrid now appears in the toolbox. Place one on Form1.NOTE: You can use the code in the remaining steps to create sample data for the grid to display.
11.Add the following code to the namespace of the Form. You can place the code either before or after the Form Class definition.
' Structure is to provide sample data for the example.
Public Structure gridData
Private mmake As String
Private myear As Integer
Public Sub New(ByVal n As String, ByVal y As Integer)
mmake = n
myear = y
End Sub
Public Property Make() As String
Get
Return mmake
End Get
Set(ByVal Value As String)
Make = Value
End Set
End Property
Public Property Year()
As Integer
Get
Return myear
End Get
Set(ByVal Value As Integer)
myear = Value
End Set
End Property
End Structure
12.Add the following code to the form class immediately following the "Windows Form Designer generated code" section:
protected dataArray(5) As gridData
13.Add the following code to the Load event of Form1:
' Create some sample data.
dataArray(0) = New gridData("ford", 1999)
dataArray(1) = New gridData("chevrolet", 1999)
dataArray(2) = New gridData("plymouth", 1988)
dataArray(3) = New gridData("honda", 1999)
dataArray(4) = New gridData("fiat", 1987) ' Assign the data to the grid. MyDataGrid1.DataSource = dataArray
14.Run the sample, and try the various keystrokes that are being trapped (UP ARROW, DOWN ARROW, TAB, CTRL+M, and ALT+Z). The caption of the form is updated to show which keystroke was pressed.
|
|
|
|
|
Rey,
Have you tried to do something simple like turning keypreview property of the form on? Inside the form's keypress/keyup events use activecontrol to know what control is receiving the keystrokes.
It's always worked for me in the past...
nathan
|
|
|
|
|
Thnak you Mr. Nathan,It is working.
With Regards
Suman
|
|
|
|
|
Hey Everyone,
I'm pretty familiar with VB.net, but I've never done anything surrounding what I'm trying to do now... I guess it would be called screenscraping. I'm using a java web based app, and what I'm trying to create is a simple app that will take data out of one text field on a form and copy it over to another text field, and then "press" a button on the form. Is any or all of this possible? If you're wondering why I'm looking to do this, the java app I'm using is a testing tool, and I want to populate the actual results text field with the expected results value, and then press the pass step button. It's kind of a pain to copy and paste every single step. So... any help?
Thanks,
Tommy
|
|
|
|
|
Since you're trying to scrape a Java app, your only option is to implement some kind of OCR to read the screen. The Java runtime doesn't use an Win32 handled controls, so there is no way you can use the tricks that would normally work on a Windows application to do the same thing in a Java window.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
I would like to create a simple datagrid in VB.net. I have a dataset that I would like to make calculations and then JUST display to the datagrid, no input needed.
Thanks.
|
|
|
|
|
dkoco,
Me.grd.DataSource = ds '<-- dataset
me.grd.DataMember = "tablename" '<-- the table name within your dataset
'---------------------------
'if you don't know the table name you can do this also
me.grd.DataMember = ds.tables(0).tablename
'---------------------------
me.grd.Refresh() '<-- makes the grid load the data in automatically for you
hope this helps
nathan
|
|
|
|
|
Hi All,
I have developed an application which contains a listview control (with 3 columns). I am able to use the LVM_FINDITEM message to finding an item from the first column of the list view control. We need to use this message to find items from the other columns in the listview control.
Shall any one please help me how to find subitems in a listview control suing LVM_FINDITEM message.
Thanks in advance,
Pradeep
|
|
|
|
|
Does it absolutely have to use LVM_FINDITEM? There are other ways of getting information from a listview.
|
|
|
|
|
Hi,
Ive created a datagrid on a form, which is filled from a dataset and sqldatadapter object.
I want to be able to modify the data in the grid, and then click Save, to post changes back to the database.
However, the wizard hasnt created an Add/Modify/Delete command in the adapter, only a select, which is something along the lines of:
SELECT ID, FirstName, LastName, address FROM Customers
I have the code under the button which says:
SqlDataAdapter1.Update(dsCustomers)
and I added an Update command to the adapter with an sql statement saying:
UPDATE Customers SET Firstname = @FirstName WHERE ID = @ID
But thats when it all goes to pot. I get an error back when I click Save, that says: Must declare the scalar variable @ID.
Also, Im thinking, I have a grid with 10 records, so how does the Updatecommand in the adapter apply its changes to each record? Is my UpdateCommand sql statement incorrect?
Cant find any MSDN walkthroughs that sort this.
Regards
Mark
|
|
|
|
|
Hi Mark,
First of all you should write if u r working on web or desktop and which version 2003 or 2005. These info will help others in giving you answers.
Waiting for your reply.
Thanks
chiranjeeb
|
|
|
|
|
|
Ok,
Ive come up with a solution to the problem.
In my save function, I modified it to:
<br />
Dim builder As New SqlClient.SqlCommandBuilder(SqlDataAdapter1)<br />
SqlDataAdapter1.UpdateCommand = builder.GetUpdateCommand<br />
SqlDataAdapter1.Update(DsCustomers)<br />
Which seems to solve the problem.
However, why doesnt the SqlDataAdapter wizard create the necessary add/update/delete commands? Is that a bug? I tried it again, on another table, and the wizard just says "there were problems trying to create the UPDATE command"
Strange
|
|
|
|
|
We are creating one porject as Desktop app with VB.NET .My problem involves in 2 cases
Case 1 :I want to write sub main() in module and i want this as startup object for the exceution but in 2005 we are unable to keep this because it will be avilable only if aplication is Console,if i keep this when i excutes it opens Console,Login page both.So how can i start execution from SUB MAIN() of module ?
Case 2:If i keep Login form as Startup object,after validating Login i wnat to display MDI form and i want to close Login form.How can i achieve this?
Please Help me
Thanking you in advance
|
|
|
|
|
Ganesh India wrote: Case 2:If i keep Login form as Startup object,after validating Login i wnat to display MDI form and i want to close Login form.How can i achieve this?
In your static void main, show the login form and if it succeeds, show the main form.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
There will be no Static Void Mian() in VB.NET its avilable in c#.net.
If we want to write sub main in Module we hage to write like
Public Sub main()
Dim x As New frmLogin
Dim y = x.ShowDialog()
If y = 1 Then
Dim frm1 As New MDIForm1
frm1.ShowDialog()
End If
'MessageBox.Show(y.ToString())
End Sub
|
|
|
|
|
Ganesh India wrote: If y = 1 Then
y = 1 ?? Is 1 the value of DialogResult.OK ? Even then, why do it this way ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
We can keep no problem ,its not the original issue after succesfully login what code i have to write to show MDI form, and what i have to do this login form ?
|
|
|
|
|
It's not the problem, it's just really bad code.
I don't get it - this code looks like it will show your MDI form, so what is the question ? Does this not work yet ? Is there no actual login form ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|