Reputation: 1303
This is my query
cmd2 = new OleDbCommand("Select TOP " + PageSize + " PatientID,PFirstName,PLastName,RegDate,MobileNo,PEmailID from Patient_Registration ORDER BY PatientID", con);
I am filling datatable with this cmd2
// con.Open();
this.adp1.SelectCommand = cmd2;
this.adp1.Fill(dt);
Now I want to merge PFirstName,PLastName
column so that I can display them as a one column in datagridview ,How can i do this
I tried something like this ,but its not working
dt.Columns.Add("FullName", typeof(string), "PFirstName + ' ' + PLastName");
Upvotes: 0
Views: 892
Reputation: 10236
The syntax to concatenate string is & You can possibly do that in your query itself.
Change your Query to
cmd2 = new OleDbCommand("Select TOP " + PageSize + " PatientID,[PFirstName]&" "&[ PLastName] as [Full Name],RegDate,MobileNo,PEmailID from Patient_Registration ORDER BY PatientID", con);
you can refer this
Upvotes: 0
Reputation: 3128
Try to do something like this
cmd2 = new OleDbCommand("Select TOP " + PageSize + " PatientID,PFirstName&' '&PLastName as 'Full Name',RegDate,MobileNo,PEmailID from Patient_Registration ORDER BY PatientID", con);
here is sql fiddle link for reference http://www.sqlfiddle.com/#!3/abc6b/1
Upvotes: 0
Reputation: 2598
You may add a Full Name column to the DataGridView and set it manually in the DataBindingComplete
event.
Like this:
DataTable dt;
private void Form1_Load(object sender, EventArgs e)
{
// sample data
dt = new DataTable();
dt.Columns.Add("FirstName", typeof(string));
dt.Columns.Add("LastName", typeof(string));
dt.Columns.Add("Age", typeof(int));
dt.Rows.Add(new object[] { "Andrew", "Jones", 5 });
dt.Rows.Add(new object[] { "Steve", "Jobs", 15 });
dt.Rows.Add(new object[] { "Surendar", "Sani", 10 });
dataGridView1.AutoGenerateColumns = false;
// Make DataGridView columns
DataGridViewTextBoxColumn dgvtxtFullName = new DataGridViewTextBoxColumn();
dgvtxtFullName.HeaderText = "Full Name";
DataGridViewTextBoxColumn dgvAge = new DataGridViewTextBoxColumn();
dgvAge.HeaderText = "Age";
dgvAge.DataPropertyName = "Age";
dataGridView1.Columns.Add(dgvtxtFullName);
dataGridView1.Columns.Add(dgvAge);
dataGridView1.DataSource = dt;
}
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
// Loop through each DataGridViewRow to set the full name column
foreach (DataGridViewRow item in dataGridView1.Rows)
{
if (!item.IsNewRow)
{
string FullName = string.Format("{0} {1}",
dt.Rows[item.Index]["FirstName"], dt.Rows[item.Index]["LastName"]);
item.Cells[0].Value = FullName;
}
}
}
Upvotes: 1