如何在datagridview中向一列添加两个mysql表列?

时间:2015-11-13 21:22:07

标签: c# mysql winforms datagridview

我需要转换这个mysql表:

FirstName | LastName | Street      | City    | State | Zipcode | Contact Number
----------+----------+-------------+---------+-------+---------+----------------
John      | Doe      | 123 W. Main | Chicago | IL    | 60600   | (312)555-7458
Jane      | Doe      | 321 W. Main | Chicago | IL    | 60600   | (312)555-6628

到datagridview中的这个表:

Name     | Address                       | Contact Name
---------+-------------------------------+--------------
John Doe | 123 W. Main Chicago, IL 60600 | (312)555-7458
Jane Doe | 321 W. Main Chicago, IL 60600 | (312)555-6628

我尝试了下面的代码,但datagridview第0列和第1列空白

string Query = "SELECT * FROM people.client";
MySqlConnection myConn = new MySqlConnection(myconnection);
MySqlCommand cmdDataBase = new MySqlCommand(Query, myConn);
MySqlDataAdapter myDataAdapter = new MySqlDataAdapter();
myDataAdapter.SelectCommand = cmdDataBase;
DataTable dbdataset = new DataTable();
myDataAdapter.Fill(dbdataset);

//Set AutoGenerateColumns False
dataGridView1.AutoGenerateColumns = false;
//Set Columns Count
dataGridView1.ColumnCount = 3;

//Add Columns
dataGridView1.Columns[0].HeaderText = "Name";
dataGridView1.Columns[0].DataPropertyName = "FirstName+ ' ' +LastName";

dataGridView1.Columns[1].HeaderText = "Address";
dataGridView1.Columns[1].DataPropertyName = "Street+ '' +City+ '' +State+ '' +Zipcode";

dataGridView1.Columns[2].HeaderText = "Contact Number";
dataGridView1.Columns[2].DataPropertyName = "Contact Number";

dataGridView1.DataSource = dbdataset;

任何帮助将不胜感激。提前谢谢。

2 个答案:

答案 0 :(得分:5)

SELECT CONCAT(FIRSTNAME, " ", LASTNAME) AS NAME,
    CONCAT(STREET, " ", CITY, ", ", STATE, " ", ZIPCODE) AS ADDRESS, PHONE
FROM people.client

选择两个字段并用空格分隔。

答案 1 :(得分:-1)

感谢Digital Chris。

Mysql表:

FirstName | LastName | Street      | City    | State | Zipcode | Contact Number
----------+----------+-------------+---------+-------+---------+----------------
John      | Doe      | 123 W. Main | Chicago | IL    | 60600   | (312)555-7458
Jane      | Doe      | 321 W. Main | Chicago | IL    | 60600   | (312)555-6628

此代码:

string Query = "SELECT idclient, CONCAT(FIRSTNAME, ' ', LASTNAME) AS NAME,"
     + " CONCAT(STREET, ' ', CITY, ', ', STATE, ' ', ZIPCODE) AS ADDRESS,"
     + "`Contact Number` FROM people.client";

MySqlConnection myConn = new MySqlConnection(myconnection);
MySqlCommand cmdDataBase = new MySqlCommand(Query, myConn);
MySqlDataAdapter myDataAdapter = new MySqlDataAdapter();
myDataAdapter.SelectCommand = cmdDataBase;
DataTable dbdataset = new DataTable();
myDataAdapter.Fill(dbdataset);

//Set AutoGenerateColumns False
dataGridView1.AutoGenerateColumns = false;
//Set Columns Count
dataGridView1.ColumnCount = 3;

//Add Columns
dataGridView1.Columns[0].HeaderText = "Name";
dataGridView1.Columns[0].DataPropertyName = "Name";

dataGridView1.Columns[1].HeaderText = "Address";
dataGridView1.Columns[1].DataPropertyName = "Adress";

dataGridView1.Columns[2].HeaderText = "Contact Number";
dataGridView1.Columns[2].DataPropertyName = "Contact Number";

dataGridView1.DataSource = dbdataset;

等于Datagridview表:

Name     | Address                       | Contact Number
---------+-------------------------------+----------------
John Doe | 123 W. Main Chicago, IL 60600 | (312)555-7458
Jane Doe | 321 W. Main Chicago, IL 60600 | (312)555-6628