I want to display data from my database using dataGridView, but I am not getting the desired result. It's showing blank spaces in the table when I run the code. Here is the code:
MySqlCommand cmd = new MySqlCommand(query, ca.getConnection());
cmd.CommandType = CommandType.Text;
MySqlDataAdapter MyAdapter = new MySqlDataAdapter(cmd);
DataSet dSet = new DataSet();
MyAdapter.Fill(dSet);
dataGridView1.DataSource = dSet.Tables[0];
The question doesn't clearly specifies what exactly the required desired result or the problem is occurring. But try providing the DataMember property of the DataGridView like in this code below-
string connectionString = "Data Source=.;Initial Catalog=pubs;Integrated Security=True";
string sql = "SELECT * FROM Authors";
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);
DataSet ds = new DataSet();
connection.Open();
dataadapter.Fill(ds, "Authors_table");
connection.Close();
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Authors_table";
Replace the last line of your code with this code may be this will be helpful once i had a same problem like your one. But my problem resolved after using this.
dataGridView1.ItemSource = dSet.Tables[0].DefaultView;
Hey listen i am retrieving data from database and i am using below code its working fine..Its SQLSERVER code. But you could get help for MYSQL from this.
SqlDataAdapter da;
con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\librarydb.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
con.Open();
da = new SqlDataAdapter("select * from student", con);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.