DataGridView

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there a way to take a datagridview which is bound to a datasource, add an
"unbound column" and bind that column to a different binding source for a
diffent table?

When I attempt to edit that columns properties, I only see the original
datasource that the datagridview is bound to.
 
Chris,

Try overriding the render method on the datagrid control to add a custom
row.

Shaun McDonnell
 
One way you could try to do thus is to turn on the VirtualMode and use
CellValueNeeded to provide values for the unbound column. If you drop
a DataGridView on a form and add a Form.Load event with this code, you
can see a popilated unbound column in the grid.

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
#region Get the DataSource
DataTable dt = new DataTable("MyTable");
int nCols = 4;
int nRows = 10;
for (int i = 0; i < nCols; i++)
dt.Columns.Add(new DataColumn(string.Format("Col{0}",
i)));

for (int i = 0; i < nRows; ++i)
{
DataRow dr = dt.NewRow();
for (int j = 0; j < nCols; j++)
dr[j] = string.Format("row{0} col{1}", i, j);
dt.Rows.Add(dr);
}
#endregion

this.dataGridView1.DataSource = dt;

DataGridViewTextBoxColumn col = new
DataGridViewTextBoxColumn();
col.Name = "UnBound";
col.HeaderText = "unbound";

this.dataGridView1.Columns.Add(col);

this.dataGridView1.VirtualMode = true;
this.dataGridView1.CellValueNeeded += new
DataGridViewCellValueEventHandler(dataGridView1_CellValueNeeded);
}

void dataGridView1_CellValueNeeded(object sender,
DataGridViewCellValueEventArgs e)
{
if (this.dataGridView1.Columns[e.ColumnIndex].Name ==
"UnBound")
{
e.Value = 100 * e.RowIndex + e.ColumnIndex;
}
}
}

==================
Clay Burch
Syncfusion, Inc.
 
Back
Top