hi guys i have existing column in datagridview sample column name (Student ID, Last Name) now i have mysql table column name sample(STUD_ID,LNAME) STUD_ID = int Type, LNAME= varchar Type.

i used these code to display mysql table in datagridview if their is no Existing column

void dlist(string sqlQry, DataGridView dgv)
    DataTable dt = new DataTable();
    MySqlCommand cmdSql = new MysqlCommand(sqlQry,Con);
    MySqlDataAdapter da = new MySqlDataAdapter(cmdSql);
    dgv.DataSource = dt.DefaultView;

My problem Is I want To display mysql table column name (STUD_ID) Value to existing datagridview Column name (Student ID) and (LNAME) to (Last Name) need some help thank you very much in the future.

In the SQL query, you can assign aliases to column names. This is probably the easiest method. I totally forget how to do this, but I'm sure google can help you out with it.

Giving a column an alias in the SQL statement is as simple:
SELECT user_id AS User FROM table_name

This will cause your dataset to populated with the data from user_id but the column will be called User in the dataset. You can do this for as many columns as you need in your SQL.

thanks guys, but do you think there's no other way to do it?

thanks guys, but do you think there's no other way to do it?

There's definetely other ways to do it. The solution proposed is the simplest, most elegant way I can think of.

You can always just enter whatever you want in the datagrid rows after the data is loaded. Don't remember off the top of my head how - I suspect theres a collection of cells (or a collection of rows with a collection of cells) that you could use to set the cell values. This would probably take a lot more effort than using the SQL method though....

hello , u can do like this , place this code before con.close();

datagridview1.column(0).headertext = "StudentID";
datagridview1.column(1).headertext = "Last Name";

M.Waqas Aslam

thank you very much Dude that's What I wanted To do

U Welcome Bro :)
M.Waqas Aslam

Edited 4 Years Ago by M.Waqas Aslam: n/a

This question has already been answered. Start a new discussion instead.