LCKB
[C#] MySQL view in listbox and some other question.. - Printable Version

+- LCKB (https://lckb.dev/forum)
+-- Forum: ** OLD LCKB DATABASE ** (https://lckb.dev/forum/forumdisplay.php?fid=109)
+--- Forum: Programmers Gateway (https://lckb.dev/forum/forumdisplay.php?fid=196)
+---- Forum: Coders Talk (https://lckb.dev/forum/forumdisplay.php?fid=192)
+---- Thread: [C#] MySQL view in listbox and some other question.. (/showthread.php?tid=808)



- Nikolee - 05-15-2012


So i was working on a Editor for T_Magic but as first the Design need to work (Display the Table)

now i can Select from a_index and show it in a DataGridView BUUT .. i wanna show it in a ListBox i tried it and it always show up "System.Data.DataRowView" (Maybe need COnvert to String?)

 

I wanna show it in a Listbox and then it load t_magic to Display the Index and the Name in the Design.

 

2

 

Uploaded with 2

 

So just if you Select X Skill it Display Name, ID , Maxlevel, Damagetyp etc..^^

 

(I filled the ComboBoxes allready)

 

Code with Datagriedview :

 

MySqlConnection con;
MySqlConnectionStringBuilder csb = new MySqlConnectionStringBuilder();
csb.Server = textBox13.Text;
csb.UserID = textBox4.Text;
csb.Database = textBox5.Text;
csb.Password = textBox6.Text;
con = new MySqlConnection(csb.ConnectionString);
MySqlConnection connection = new MySqlConnection(csb.ConnectionString);
connection.Open();

string Test = textBox2.Text;
String sql1 = "SELECT * FROM t_magic a_index";
DataTable datatable = new DataTable();
MySqlDataAdapter dataAdapter = new MySqlDataAdapter(sql1, connection);
dataAdapter.Fill(datatable);
dataGridView1.DataSource = datatable;
dataGridView1.Show();
dataGridView1 .Update();

[/Code]

 
And Code for ListBox with Fail :
 
[Code]
MySqlConnection con;
MySqlConnectionStringBuilder csb = new MySqlConnectionStringBuilder();
csb.Server = textBox13.Text;
csb.UserID = textBox4.Text;
csb.Database = textBox5.Text;
csb.Password = textBox6.Text;
con = new MySqlConnection(csb.ConnectionString);
MySqlConnection connection = new MySqlConnection(csb.ConnectionString);
connection.Open();

string Test = textBox2.Text;
String sql1 = "SELECT * FROM t_magic a_index";
DataTable datatable = new DataTable();
MySqlDataAdapter dataAdapter = new MySqlDataAdapter(sql1, connection);
dataAdapter.Fill(datatable);
listBox1.DataSource = datatable;
listBox1.Show();
listBox1.Update();

 

2

 

Uploaded with 2




- Nikolee - 05-15-2012



foreach (DataRow row in datatable.Rows)
{
ListViewItem item = new ListViewItem(
new string[] {
row["a_index"].ToString(),
row["a_level"].ToString(),
row["a_hitrate"].ToString()});
listView1.Items.Add(item);
}

 

but i need listbox ^^




- someone - 05-15-2012


Create a class Entity for the database Table

class t_magic{
//class properties
//depending on the method of access if you dont wan
private int a_index;
private string a_name;
//...
//class methods interface methods(add this if the properties are private)
public int Index{
get{
return a_index;
}
set{
a_index = value;
}
}
public string Name{
get{
return a_name;
}
set{
a_name = value;
}
}
//...
}

 

Then create a linked list for the t_magic class:

//...
//public or private depends on how you want to access it.
private Static LinkedList t_magicList = new LinkedList();
//....
//note mysqlclass does not exist if you will look for it its something invented on the spot Just for this example.
for(int i = 0;i < mysql.QuerySize;i++){
t_magic magic = new t_magic();
magic.Index = mysql.getInt("a_index");
magic.Name = mysql.getString("a_name");
//...
t_magicList.add(magic);
mysql.nextRow();
}

 

And inserting the data into list would be:

List myList = new List();

for(int i = 0;i < t_magicList.Count; i++){
t_magic magic = new t_magic();
magic = t_magicLis[i];
myList.add(magic.Index + " "+magic.Name);
}
listbox1.DataSource =null; //empty listbox1
listbox1.DataSource = myList; //add new data to it

 

According to Selected index on the listbox1 you could edit the data from t_magicList, and then send it to the database.




- Wizatek - 05-15-2012



foreach (DataRow row in datatable.Rows)
{
ListViewItem item = new ListViewItem(
new string[] {
row["a_index"].ToString(),
row["a_level"].ToString(),
row["a_hitrate"].ToString()});
listView1.Items.Add(item);
}

 

That was close, but its much easyer.

 

foreach (DataRow row in datatable.Rows)
listBox1.Items.Add(row["a_index"] + " - " + row["a_name"]);

 

Personally i wouldnt use the datatables and such.

Although they were made for it i myself would prefer using generic lists and use my own querys




- SibaN - 05-17-2012

realy hmm i dont understand anything here lol xD