0

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)
{
    Con.Open();
    DataTable dt = new DataTable();
    MySqlCommand cmdSql = new MysqlCommand(sqlQry,Con);
    MySqlDataAdapter da = new MySqlDataAdapter(cmdSql);
    da.Fill(dt);
    dgv.DataSource = dt.DefaultView;
    Con.Close();
}

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.

5
Contributors
9
Replies
10
Views
5 Years
Discussion Span
Last Post by M.Waqas Aslam
0

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.

0

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.

0

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

0

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....

0

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

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

Regards
M.Waqas Aslam

0

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

U Welcome Bro :)
regards
M.Waqas Aslam

Edited by M.Waqas Aslam: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.