|
Thanks Hessam Jalali, yet to try this, can you explain little bit briefly.
|
|
|
|
|
I need to add row in DatagridView based on array of business entities.
I'm using NHibernate.
NHiberante returns array of business objects.
So my source type is System.Array and I cannot change it.
Joe Stegman "DataBinding FAQ - Updated.doc" describes that BindingList<t>
AddingNew event should be used in this case.
Also there is CoreAddNew method which is not documented in VCS 2005 Express
documentation.
I expect that BindingList<t> or some of its implementation can maintain
separate list of added objects by implementing custom Add() method.
How to fix ?
Andrus.
Using .NET 2.
Code to reproduce:
using System.Windows.Forms;<br />
using System.Collections.Generic;<br />
using System.ComponentModel;<br />
<br />
class TestApplication {<br />
static void Main() { Application.Run(new TestForm()); }<br />
}<br />
<br />
class TestForm : Form {<br />
<br />
DataGridView DataGridView = new DataGridView();<br />
<br />
public TestForm() {<br />
Controls.Add(DataGridView);<br />
<br />
BindingSource BindingSource = new BindingSource();<br />
IList<Customer> list = new Customer[0];<br />
BindingList<Customer> bindingList = new BindingList<Customer>(list);<br />
BindingSource.DataSource = bindingList;<br />
<br />
BindingSource.AddingNew += delegate(object sender, AddingNewEventArgs e) {<br />
e.NewObject = new Customer();<br />
};<br />
<br />
DataGridView.DataSource = BindingSource;<br />
}<br />
}<br />
<br />
class Customer {<br />
string name;<br />
public string Name {<br />
get { return name; }<br />
set { name = value; }<br />
}<br />
}
Observed:
System.NotSupportedException was unhandled
Message="Collection is read-only."
Source="mscorlib"
StackTrace:
at System.ThrowHelper.ThrowNotSupportedException(ExceptionResource
resource)
at
System.Collections.ObjectModel.Collection`1.System.Collections.IList.Add(Object
value)
at System.Windows.Forms.BindingSource.Add(Object value)
at System.Windows.Forms.BindingSource.AddNew()
at System.Windows.Forms.CurrencyManager.AddNew()
at
System.Windows.Forms.DataGridView.DataGridViewDataConnection.AddNew()
at
System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnNewRowNeeded()
at System.Windows.Forms.DataGridView.OnRowEnter(DataGridViewCell&
dataGridViewCell, Int32 columnIndex, Int32 rowIndex, Boolean
canCreateNewRow, Boolean validationFailureOccurred)
at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32
columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean
validateCurrentCell, Boolean throughMouseClick)
at
System.Windows.Forms.DataGridView.SetAndSelectCurrentCellAddress(Int32
columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean
validateCurrentCell, Boolean throughMouseClick, Boolean clearSelection,
Boolean forceCurrentCellSelection)
at
System.Windows.Forms.DataGridView.MakeFirstDisplayedCellCurrentCell(Boolean
includeNewRow)
at System.Windows.Forms.DataGridView.OnEnter(EventArgs e)
at System.Windows.Forms.Control.NotifyEnter()
at System.Windows.Forms.ContainerControl.UpdateFocusedControl()
at
System.Windows.Forms.ContainerControl.AssignActiveControlInternal(Control
value)
at
System.Windows.Forms.ContainerControl.ActivateControlInternal(Control
control, Boolean originator)
at
System.Windows.Forms.ContainerControl.SetActiveControlInternal(Control
value)
at System.Windows.Forms.ContainerControl.SetActiveControl(Control
ctl)
at System.Windows.Forms.ContainerControl.set_ActiveControl(Control
value)
at System.Windows.Forms.Control.Select(Boolean directed, Boolean
forward)
at System.Windows.Forms.Control.SelectNextControl(Control ctl,
Boolean forward, Boolean tabStopOnly, Boolean nested, Boolean wrap)
at System.Windows.Forms.Control.SelectNextControlInternal(Control
ctl, Boolean forward, Boolean tabStopOnly, Boolean nested, Boolean wrap)
at System.Windows.Forms.Form.set_Active(Boolean value)
at System.Windows.Forms.Form.WmActivate(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&
m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd,
Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at TestApplication.Main() in C:\test\test\Program.cs:line 20
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
*/
Andrus
|
|
|
|
|
Good evening!
I would like to add OnClick-events to all button-controls within a form which contains a single split-container. I'll try to explain it:
I have a Windows form which includes one control, a spli-container, which itself has two panels. In one of these there are several buttons. All of them should subscribe to a click-event during initialization process.
I tried it with
[code]
foreach (Control in this.Controls)
{
if (cntr is Button)
{cntr.Click += new EventHandler(delegate(object sender, EventArgs e) {MessageBox.Show(cntr.Name);});}
}
[/code]
but "this.Controls" contains only the split-container.
I know that in this very special example I could as well enumerate through the panels of the split-container, but I would like to use a solution which is independent of the amount of the panels.
I think a possibility is needed, which allows to check if a control contains sub-controls and if so, one of these is a button type control.
Help would be appriciated!
|
|
|
|
|
you can use recursive method (as deep first search or stack search)
it would be somewhat like this
void AssociateClick(Control starterControl)
{
if(starterControl==null)return;
foreach(Control c in starterControl.Controls)
{
if(c is Button)
c.Click+=new EventHandler(c_Click);
AssociateClick(c);
}
}
void c_Click(object sender, EventArgs e)
{
MessageBox.Show((sender as Button).Name);
}
if you pass the form as starter it would iterate in all controls and if iterated control is a button the c_Click method would be associated to that
and of course it can be implemented with other methods like Breadth search (with Q) but maybe recursive is the easiest way in this case
hope the post would be useful
good luck
|
|
|
|
|
Thank you! Works like a charm. Now as I see it it seems logical.
|
|
|
|
|
Hi,
Iam trying to open a word document using filestream and thereby put the data in byte array by reading the filestream.
Now, i want to write back the byte stream to a file like doc file, would there be any complexities by doing this?
If the document has some images can it be written back to a file?
code is as follows:
//for reading
FileStream fileStream = new FileStream(fileName, FileMode.OpenOrCreate,
FileAccess.Read);
long len;
len = fileStream.Length;
Byte[] fileAsByte = new Byte[len];
fileStream.Read(fileAsByte, 0, fileAsByte.Length);
same logic for writing too
Thanks,
gauthee
|
|
|
|
|
Hi,
you can read all bytes of all files you can access.
and you can write these bytes again to any location you can access.
Since .NET 2.0 you can do it with File.ReadAllBytes and File.WriteAllBytes,
you don't need streams for that.
Don't really know what good it all will do for you tho. You probably will
not be able to understand the bytes, nor change them in any way that
results in a valid new document. And you can't just convert a document
by changing its file extension, if that were what you hope for.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Hi All,
I have got a problem in events. I have a treeview Before_Select event and one is Panel_leave event
so when i m on panel and i click a treenode the Panel_leave is executed but Before_Select doesn't execute .But if i make a break point in Panel_leave it works and if i remove break points it doesn't work.
Please help in solving this problem , I think i need thread Synchronization , but i don't know how to do it.
Thanx.
Praveen Sharma
|
|
|
|
|
Hi,
the events go to the active message pump, that is the GUI trhead of the
currently active window (which normally means: the main thread for all windows).
So Panel_leave and Before_Select normally get handled by the same thread,
that will not be the problem.
Are you sure Before_Select does not fire? How did you figure that?
Best way is: add log statements such as log("begin of Before_Select");
and provide a log method in every interested class, e.g.
public static void log(string s) {
s=DateTime.Now.ToString("hh:mm:ss.fff")+" "+s;
Console.WriteLine(s);
}
If however you try to manipulate some controls from inside another thread,
then you must use Control.InvokeRequired property and Control.Invoke() method;
if not, all kinds of strange things may and will happen.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Hi!
I have something like this:
public class MyApp: System.Windows.Application
{
public MyApp()
{
m_Window = new Window();
}
Window m_Window;
}
and it gets instantiated from:
class Program
{
[STAThread]
static void Main()
{
MyApp app = new MyApp();
app.Run();
}
}
Every time someone says m_Window.Close() the whole application gets terminated ( = also 'app' gets terminated).
But that is not what I want, I want only to close m_Window ( = 'app' should keep running).
So, where did I go wrong?
Thanks,
Mario M.
|
|
|
|
|
It Works when I derive MyApp from System.Windows.Window but it looks quite ugly:
public class MyApp : Window
{
public MyApp()
{
this.Visibility = Visibility.Hidden;
m_Window = new Window();
}
Window m_Window;
}
class Program
{
[STAThread]
static void Main()
{
Application app = new Application();
MyApp win = new MyApp();
app.Run();
}
}
|
|
|
|
|
m_Window.Close() is different than Visibility.Hidden now isn't it.
In the first case, yes, the application will terminate because the form has been closed, thus terminating the message loop. In the second case the window is not closed, just hidden, so the message loop continues and the application does not terminate.
only two letters away from being an asset
|
|
|
|
|
So what's your point?
In the first case I derived it from 'Application' and in the second I derived it from 'Window'.
But I don't want to derive it from 'Window' because it is no window.
|
|
|
|
|
The point is, learn the difference between window.close and window.hidden
only two letters away from being an asset
|
|
|
|
|
How about looking at the two DIFFERENT code samples I posted?
In the first snipped I create an App that has a window as member (only one window). When the window gets closed also the app terminates.
And in the second sample I create a hidden window that has an additional window as member (two windows). I don't say: m_window.hidden! I hide the parent!
But I think creating a hidden window just to make sure that the class itself survives when one of its members gets closed is quite ugly.
So, please have a look at the code first, before you start getting wise!
Mario M.
|
|
|
|
|
MarioMARTIN wrote: before you start getting wise!
Too late, already am.
only two letters away from being an asset
|
|
|
|
|
Hi,
a normal window app has an Application.Run(new Form1()); in its static main method.
The Run method will show the Form1 instance (and run a message loop to do that),
and it will return when the form gets closed or Application.Exit() gets called.
as an alternative you can call Application.Run(); without arguments;
that one will start a message pump and return when Application.Exit() gets called.
Hope this helps.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
"as an alternative you can call Application.Run(); without arguments;"
yeah thanks, but I think taht is what I showed already in my first posting.
Maybe I should try it verbally:
1.) I want an application that instantiates a class
2.) This class should open a window
3.) When the window gets closed the class should stay 'alive' (and therefore also the application must not get terminated)
Mario M.
|
|
|
|
|
Hi,
one way of doing that is:
- create a Windows app with a main form
- hide the main form (it initially might have served as a splash screen)
- create another window/form/whatever, maybe inside the main form's Load event
- do whatever you want with that window/form/whatever, closing it does not
affect the (invisible) mainform, hence the app continues (doing what I might ask).
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Forget it, I'll take the "derive from Window" approach
Thanks,
Mario M.
|
|
|
|
|
Hello,
MarioMARTIN wrote: Forget it,
No! why?
It's interesting and might help somebody else if cleared up!
MarioMARTIN wrote: I'll take the "derive from Window" approach
Which, is what Luc pointed out, very well explaint, here![^]
He got my '5' for that!
All the best,
Martin
|
|
|
|
|
Hi!
"Which, is what Luc pointed out, very well explaint, here!"
And what I already posted here
Ciao
Mario M.
|
|
|
|
|
Hello,
MarioMARTIN wrote: And what I already posted here
I know! And I think Luc did also read your post, but wanted to clear it up for you and others!
All the best,
Martin
|
|
|
|
|
Problem solved:
[STAThread]
static void Main()
{
Application app = new Application();
app.ShutdownMode = ShutdownMode.OnExplicitShutdown;
MyApp win = new MyApp();
app.Run();
}
class MyApp
{
public MyApp()
{
m_Win = new Window();
m_Win.ShowDialog();
}
Window m_Win;
}
}
|
|
|
|
|
i can't connect sql 2000 for visual studio 2005 verssion.
|
|
|
|
|