|
how would one generate a listbox with items from his dataset?
for example.
I have a dataset with a table Movie and a column Title.
I'm using oleDbDataAdapter1 on a access database.
I want my list box to contain all the titles from that database.
I don't know much about sql yet. so pardon my ignorance
Win32newb
"Making windows programs worse than they already are"
|
|
|
|
|
See the ListControl.DataSource property documentation in the .NET Framework SDK, from which the ListBox derives. This has nothing to do with SQL itself. Once you get a DataSet that contains your data from a database (or other OLE DB or ODBC source), then it's simple:
listBox1.DataSource = ds;
listBox1.DataMember = "MyTable";
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "ID"; You can set the last three initially after you instantiate the ListBox (called listBox1 in this simple example). You can set DataSource at any time when you get data to fill the ListBox . You do not have to re-create it or anything, just re-assign a new DataSet (or other data source, like an array or a collection - anything that implements IList or IListSource ).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks for your help that helped alot. I appreciate it.
Win32newb
"Making windows programs worse than they already are"
|
|
|
|
|
One quick question.
I used a combobox instead but anyway i have my values in there.
I can't figure out what the actually texts value gets stored as.
for example.
I have
A
B
C
If i select B I have MessageBox.Show(comboBox1.SelectedText);
I thought this would show the text that was selected but all i get is System.Data.blah.blah blah
Any idea why this is?
Win32newb
"Making windows programs worse than they already are"
|
|
|
|
|
Depending on the value of ComboBox.DropDownStyle , the SelectedText is not what you want to use. If DropDownStyle is set to ComboBoxStyle.DropDownList , then use the SelectedIndex to get the index and use ComboBox.Items[index] to get the item you want.
If you're getting a System.Data.DataRowView (or similar), then you aren't binding your ComboBox to your DataSet correctly. The code I wrote above still works the same, but I can't help you unless you post the code you're using to initialize your ComboBox .
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I figured it out.
All i needed to do was comboBox1.Text
I forgot about the text option.
that way i can say
oleDbDataAdapter1.SelectCommand.Parameters["Title"].Value = comboBox1.Text;
thanks for the help
Win32newb
"Making windows programs worse than they already are"
|
|
|
|
|
I have a question.
I have the listbox showing all my items now. and have it linked to the database and it is removing the items. However. When i remove the item i want the listbox to be updated. its not doing it though.
private void removebutton_Click(object sender, System.EventArgs e)
{
oleDbDataAdapter1.Fill(dataset);
try
{
string strSQL = "DELETE FROM Movie WHERE Title = '" + comboBox1.Text.ToString() + "'";
oleDbDeleteCommand1.CommandText = strSQL;
oleDbDataAdapter1.DeleteCommand = oleDbDeleteCommand1;
oleDbDeleteCommand1.Connection = oleDbConnection1;
oleDbDeleteCommand1.Connection.Open();
oleDbDeleteCommand1.ExecuteNonQuery();
oleDbDeleteCommand1.Connection.Close();
MessageBox.Show(comboBox1.Text.ToString() + " has been removed");
oleDbDataAdapter1.Update(dataset);
}
catch (Exception ex)
{
MessageBox.Show("Not found");
MessageBox.Show(ex.ToString());
}
}
Win32newb
"Making windows programs worse than they already are"
|
|
|
|
|
You updated the database using the changes in the DataSet (known as a DiffGram). As I mentioned previously, re-assign the new DataSet to the DataSource property (leaving the other properties untouched - they do not need to be changed).
Note, though, that if you remove an item from the ListBox - which removes the item from the DataSet - then why bother rebinding? It's already removed from the ListBox . This is a big waste of time, unless you know the database may be updated by other clients.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath thanks for your help. I'm just not getting it for some reason. I have read what you typed several times and its just not clickin for some reason.
From what I gather you do whatever you want to with the dataset like i'm doing, then when your done messing around with it you then update it with
OleDbDataAdapter1.Update(dataset);
so based on what i'm gathering from you. I type the following to refesh my comboBox
comboBox1.DataSource = dataset; // update the comboBox
if that is the correc then for some reason its not working.
private void removebutton_Click(object sender, System.EventArgs e)
{
try
{
string strSQL = "DELETE FROM Movie WHERE Title = '" + comboBox1.Text.ToString() + "'";
oleDbDeleteCommand1.CommandText = strSQL;
oleDbDataAdapter1.DeleteCommand = oleDbDeleteCommand1;
oleDbDeleteCommand1.Connection = oleDbConnection1;
oleDbDeleteCommand1.Connection.Open();
oleDbDeleteCommand1.ExecuteNonQuery();
MessageBox.Show(comboBox1.Text.ToString() + " has been removed");
oleDbDataAdapter1.Update(dataset); oleDbDeleteCommand1.Connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Not found");
MessageBox.Show(ex.ToString());
oleDbDeleteCommand1.Connection.Close();
}
oleDbDataAdapter1.Fill(dataset);
comboBox1.DataSource = dataset;
}
sorry Just not understanding this
Win32newb
"Making windows programs worse than they already are"
|
|
|
|
|
For one, if the item is removed in the data-bound control - which removes it from the DataSet bound to the control - and you update the database, why would you need to update the data-bound control? Think about it - the item is already removed. Why waste the time re-binding when the information is already in sync?
Second, if you want to assign the same DataSet to the DataSource (it's still the same instance), you must reset the binding by first setting DataSource to null then back to the DataSet instance.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Ok i got it working.
I did used the clear() on the dataset and then refilled it. I'm glad Code Project is around and folks like yourself that give me a hint but not the straight answer. That way I'm forced to learn it.
thanks again,
Win32newb
"Making windows programs worse than they already are"
|
|
|
|
|
Has anyone came up with a solution of how to change the toolbars default color? I noticed there is a toolbar.defaultbackcolor; but that just shows its value. How do you set it. I also read on msdn something about.
private virtual Color BackColor {get; set;} but thats pretty much all it says or i'm not getting it one. I'm assuming you have to override it somehow. I have looked around i see premade classes and all but id really like to know how its done instead of just having someone elses premade stuff. Is it highly complex? if not could someone post a simple example?
Win32newb
"Making windows programs worse than they already are"
|
|
|
|
|
ToolBar.BackColor is overridden and attributed both with the BrowsableAttribute and EditorBrowsableAttribute to hide it from designer controls (like the PropertyGrid) and source editors. Why? Because it's not used. If you want to change the background color of the ToolBar , you have to handle all the Win32 notification messages by overriding WndProc (experience with Windows messaging will be helpful) or use a third-party toolbar, from which you'll get a lot more options than the simple one included in the .NET FCL (Framework Class Library).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks. I check in on it I knew there had to be a reason it wasn't implemented. Thanks again.
Win32newb
"Making windows programs worse than they already are"
|
|
|
|
|
Ok let me explain what i'm trying to do first off
I have 1 form called form1 then i have 2 user controls add and delete.
I have a menu put on forms1 cause i'm using it as my main interface.
I have on the menu a item add record and then remove record.
when i hit the add record i want to bring to focus the add();
and the same thing for if i hit delete i want delete() to bringtofront.
But I don't want numerous instances of the same thing so how do i kill the instances based on the menu selection?
I never want form1 to be killed i just want it to loose its focus based on the menu selection.
So if i have in form1
private database.Add objSearch_notfound;
and under the addmenu
objSearch_notfound = new Add();
Controls.Add(objSearch_notfound);
objSearch_notfound.BringToFront();
objSearch_notfound.Show();
I understand this will bring the add to focus.
I also understand that
Controls.Remove(this);
this.hide();
if put on a button in the add() will kill the add() focus.
But how do i make it do this on the menu?
cause when i click the menu then the add() loses focus so if i did
addmenu()
controls.Remove(this);
this.hide();
it will kill the form1
I thought i could in form1 do this but it doesn't work
Controls.Remove(objsearch_notfound)
objsearch_notfound.Hide();
any clues would be great and hope this is clear. It's kinda hard to explain without showing you the source code.
Win32newb
"Making windows programs worse than they already are"
|
|
|
|
|
Showing and hiding user controls or forms will not make dispose of them. Just use a couple of fields in your class to keep a reference to them, the same as the designer does when you design your form:
public class MyForm : Form
{
private Add addControl;
private Delete deleteControl;
public MyForm()
{
addControl = new Add();
deleteControl = new Delete();
}
private void addMenu_Click(object sender, EventArgs e)
{
Controls.Remove(deleteControl);
Controls.Add(addControl);
}
private void deleteMenu_Click(object sender, EventArgs e)
{
Controls.Remove(addControl);
Controls.Add(deleteControl);
}
} This is a simple example, but hopefully gives you some idea that you don't dispose of controls when showing/hiding them, or adding/removing them from the parent's Controls collection property.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
First off thanks this worked perfectly. Well kinda don't know if its just forgotten in your snippet or i'm doing something wrong. In any event. If you don't do addcontrol.BringToFront(); then it simply overlays the other window looking like crap Just something i seen when playing around with.
Thanks again for the help.
but the object ref works great i thought that was how it was done but just couldn't figure it out. thanks again.
Win32newb
"Making windows programs worse than they already are"
|
|
|
|
|
It wasn't "forgotten". Remember, this was sample code. Don't expect a complete solution from sample code: nothing is truly learned then.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
One approach that you could take (if it fits in with your application design) is to make the main form an MdiParent and make the other forms MdiChild forms.
When either choice is clicked the first step each option would take is to issue a CloseAllChildren() call. The logic for the parent form for this method is as follows:
private void CloseAllChildren()
{
System.Windows.Forms.Form[] myChildren = this.MdiChildren;
foreach (Form child in myChildren)
{
child.Close();
}
return;
}
If you children have any valuable resources that they should release, you can add a trap on the Closing Event.
public void Form1_Closing(object sender, System.EventArgs e)
{
... release resources ...
... if user did not save see if they really want to throw
awway their work
e.CloseCancel = true;
}
___________________
Forgoing antagonism and separation, one enters into the harmonious oneness of all things. Lao Tzu
|
|
|
|
|
First of all thankx for a reply. Well, The problem is that as soon as I put a FlashCom control like the Shockwave Flash Object Control that comes with FLASH MX.
As I put the name of the .swf file and play a movie. The .NET Framework Crashes. It Restarts the Framework.
The error it gives is this:
Object Reference not set to an instance of na object.
Well, I am working on a Windows BAsed Application and therefore it is causing much Headache for me.
DO reply and come up with a favourabce Soloution.
Imad
Imad
|
|
|
|
|
This is not helpful at all. The exception message is plain enough to understand: an object reference (i.e., a variable on which you're trying to call a method or access a property) is null , so you get a NullReferenceException . That in itself is not helpful to anyone but you. Debug your code properly, find out where it's happening. If you can't figure it out after that, then post some sample code.
It's like telling a doctor you have pain without telling him/her where it hurts.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
"But Doc, I'm dying here! Fix me!"
Jeremy Kimball
Moderation is for monks. -Lazarus Long
And this, too, shall pass away...
|
|
|
|
|
I want to control windowskey programatically.I saw sendkeys class in C#,but there are no technique in it to control windows key.please tell me any tech,and give me sample code for this.
mughalali
|
|
|
|
|
Throw a simple program together and discover what key data is used:
using System;
using System.Windows.Forms
public class Test : Form
{
static void Main()
{
Application.Run(new Test());
}
protected override void OnKeyDown(KeyEventArgs e)
{
Console.WriteLine(e.KeyValue);
base.OnKeyDown(e);
}
} The left Windows key turns out to be 91, while the right is 92. As I mentioned in the previous post, you can P/Invoke PostMessage and send WM_KEYDOWN (0x0100) to send this key code. See the Platform SDK for more information.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
how i use postmessage in c# to turn on|of the windows left key,give me sample code
mughalali
|
|
|
|