Binding to Specific Datagridview Columns That I Create in Code

  • Thread starter Thread starter David_W_1969
  • Start date Start date
D

David_W_1969

Guest
Hello all. Before I get into it, I will show my code.

public struct KEYTABLES
{
public string TableName;
public int MaxInterval;

}

public class KeyTableXML
{
public string m_TableName;
public int m_Interval;

public KeyTableXML(string tn, int interval)
{
m_TableName = tn;
m_Interval = interval;
}

public string KeyTable
{
get { return m_TableName; }
}

public int Interval
{
get { return m_Interval; }
}
}

public class KTXML
{
private List<KeyTableXML> m_KTxml;
public KTXML(List<KEYTABLES> kt)
{
m_KTxml = new List<KeyTableXML>();

foreach (KEYTABLES k in kt)
{
KeyTableXML ktxml = new KeyTableXML(k.TableName, k.MaxInterval);
m_KTxml.Add(ktxml);
}
}

public List<KeyTableXML> GetData()
{
return m_KTxml;
}
}


private void LoaddgKeyTables()
{
// Bind the BindingSource to the DataGridView
// controls DataSource.
this.dgKeyTables.DataSource =
this.bindingSource1;
KTXML x = new KTXML(KeyTables);
bindingSource1.DataSource = x.GetData();


DataGridViewComboBoxColumn col0 = new DataGridViewComboBoxColumn();
col0.HeaderText = "Table Name";

foreach (string table in AllInitialDBTables)
{
col0.Items.Add(table);
}

dgKeyTables.Columns.Add(col0);

DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
col.HeaderText = "Interval";
dgKeyTables.Columns.Add(col);

// Add a DemoCustomer to cause a row to be displayed.
this.bindingSource1.AddNew();
}



My goal is to have a DataGridView with 2 columns.

Column 1 should be a listbox column that displays TableName. The listbox itself shows all available tables from List<KeyTable>. The listbox is being populate correctly from the foreach loop.

Column 2 should be a textbox column that displays the Interval.


What I get with this code is a datagrid with 4 columns. I get a the 2 bound columns that are plain text boxes that show the proper values. Then I get the 2 columns that I created in code that show nothing. The Table column is a listbox and the listbox is populated with table names.

Can someone help me figure out what I am doing wrong? I want the datagrid columns that I create in code to show the data from the binding source. I have tried binding the datagrid to the binding source before I create the columns, and after I create the columns.

Thanks,

David


I figured it out and wanted to show the solution. I found the solution from http://stackoverflow.com/questions/2648921/c-adding-columns-to-bound-datagridview-with-code

private void LoaddgKeyTables()
{
DataGridViewComboBoxColumn col0 = new DataGridViewComboBoxColumn();
col0.HeaderText = "Table Name";

foreach (string table in AllInitialDBTables)
{
col0.Items.Add(table);
}
col0.DataPropertyName = "KeyTable";
dgKeyTables.Columns.Add(col0);

DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
col.HeaderText = "Interval";
col.DataPropertyName = "Interval";
dgKeyTables.Columns.Add(col);

dgKeyTables.AutoGenerateColumns = false;
// Bind the BindingSource to the DataGridView
// controls DataSource.
this.dgKeyTables.DataSource =
this.bindingSource1;
KTXML x = new KTXML(KeyTables);
bindingSource1.DataSource = x.GetData();

// Add a DemoCustomer to cause a row to be displayed.
//this.bindingSource1.AddNew();
}

I needed to add the DataPropertyName to each column that I built. This is how the datagridview columns get associated with a field in the binding source.

Continue reading...
 
Back
Top