Hi,
I'm using a ComboBox, some Textboxes, and a DataGrid to represent a
many-to-many relationship between Person and Course. Each time that I change
the value in the ComboBox (which for now is the OID of Person), the
information of the person matching the selected OID is shown in the Textboxes
(Name, Address, id, etc) and the courses this person is taken are shown in a
DataGrid (course name, price, etc.). This is working well so far, I just used
the code shown in this article:
*Displaying Many-to-Many Relationships
http://www.developerfusion.co.uk/show/4491/
Now I want to make my DataGrid look nicer by auto resizing the Columns
according to its largest entry. So, I used the code mentioned here:
* 5.52 How can I autosize a column in my datagrid?
http://www.syncfusion.com/FAQ/Window...44c.aspx#q877q
I called the method mentioned in the previous faq (AutoSizeTable) in the
combobox's SelectedIndex Event handler. But it seems that when the
SelectedIndex is changed, the DataGrid still has the previous data. I also
tried with the SelectionChange Committed, but I got the same results. How can
I solved this problem?
Thanks in advanced,
Josef
Here is the code I'm using:
private void FillDataSet(Ole DbConnection conn, DataSet dataSet, string
tableName)
{
string selectCommand;
OleDbDataAdapte r dbAdapter;
dbAdapter = new OleDbDataAdapte r();
selectCommand = "SELECT * FROM " + tableName;
dbAdapter.Selec tCommand = new OleDbCommand(se lectCommand);
dbAdapter.Selec tCommand.Connec tion = conn;
dbAdapter.Fill( dataSet,tableNa me);
}
//This peace of code autosizes a DataGrid columns to fit the size of the
current
//data.
public void AutoSizeTable(D ataGrid grid)
{
DataGridTableSt yle tableStyle;
tableStyle = grid.TableStyle s[0];
if ((grid.DataSour ce != null) && (tableStyle != null))
{
int numCols = tableStyle.Grid ColumnStyles.Co unt;
for(int i = 0; i < numCols; ++i)
AutoSizeCol(gri d,i);
}
}
public void AutoSizeCol(Dat aGrid grid, int col)
{
float width;
CurrencyManager childCurrencyMa nager;
CurrencyManager parentCurrencyM anager;
DataGridTableSt yle tableStyle;
tableStyle = grid.TableStyle s[0];
childCurrencyMa nager =
(CurrencyManage r)grid.BindingC ontext[DBDataSet,grid. DataMember];
parentCurrencyM anager =
(CurrencyManage r)grid.BindingC ontext[DBDataSet,"Pers on"];
int numRows = childCurrencyMa nager.Count;
Graphics g = Graphics.FromHw nd(grid.Handle) ;
StringFormat sf = new StringFormat(St ringFormat.Gene ricTypographic) ;
SizeF size;
//First gets the width of the header. Sometimes the header is longer as the
//data.
size = g.MeasureString (
tableStyle.Grid ColumnStyles[col].HeaderText,
grid.Font, 500, sf);
width = size.Width;
for(int i = 0; i < numRows; ++ i)
{
size = g.MeasureString (grid[i, col].ToString(), grid.Font, 500, sf);
if(size.Width > width)
width = size.Width;
}
g.Dispose();
tableStyle.Grid ColumnStyles[col].Width = (int) width + 16;
}
private void InitGridStyles( DataGrid grid)
{
CurrencyManager currencyManager ;
DataGridTableSt yle tableStyle;
currencyManager =
(CurrencyManage r) BindingContext[DBDataSet, "Person.PersonC rossTable"];
tableStyle = new DataGridTableSt yle(currencyMan ager);
// Add the table style to the collection of a DataGrid.
grid.TableStyle s.Clear();
//Removes the Person_OID column
tableStyle.Grid ColumnStyles.Re move(tableStyle .GridColumnStyl es[0]);
grid.TableStyle s.Add(tableStyl e);
}
private void frmDataBinding_ Load(object sender, System.EventArg s e)
{
DataColumn parentCol;
DataColumn childCol;
DataColumn[] primaryKeys;
DataRelation relationShip;
//First a connection to a access database is created
DBConnection = new OleDbConnection ("Provider=Micr osoft.Jet.OLEDB .4.0;"+
@"Data Source=C:\Persi stentLayer\Data base\test_many-to-many.mdb");
try
{
//We open the connection
DBConnection.Op en();
//The DataSet is created
DBDataSet = new DataSet();
//We fill the record set with the Person records
FillDataSet(DBC onnection,DBDat aSet,"Person");
primaryKeys = (DataColumn[])Array.CreateIn stance(typeof(D ataColumn),1);
primaryKeys[0] = DBDataSet.Table s["Person"].Columns["OID"];
DBDataSet.Table s["Person"].PrimaryKey = primaryKeys;
//We fill the DataSet with the Courses and the cross refference table
//CoursePerson
FillDataSet(DBC onnection,DBDat aSet,"Course");
FillDataSet(DBC onnection,DBDat aSet,"CoursePer son");
//Then we set the relationship between Person and CoursePerson
parentCol = DBDataSet.Table s["Person"].Columns["OID"];
childCol = DBDataSet.Table s["CoursePers on"].Columns["Person_OID "];
relationShip = new
DataRelation("P ersonCrossTable ",parentCol,chi ldCol,true);
relationShip = new DataRelation("P ersonCrossTable ",parentCol,chi ldCol);
DBDataSet.Relat ions.Add(relati onShip);
//Finally the relation between CoursePerson and Course is being set
parentCol = DBDataSet.Table s["Course"].Columns["OID"];
childCol = DBDataSet.Table s["CoursePers on"].Columns["Course_OID "];
relationShip = new
DataRelation("C ourseCrossTable ",parentCol,chi ldCol,true);
DBDataSet.Relat ions.Add(relati onShip);
//Some calculated rows are added
DBDataSet.Table s["CoursePers on"].Columns.Add("n ame",typeof(str ing),"Parent(Co urseCrossTable) .name");
DBDataSet.Table s["CoursePers on"].Columns.Add("p rice",typeof(st ring),"Parent(C ourseCrossTable ).price");
}
catch (InvalidOperati onException exception)
{
MessageBox.Show ("Connection is already open: " + exception.Messa ge);
}
catch (OleDbException exception)
{
MessageBox.Show ("Ole Exception: " + exception.Messa ge);
}
catch (Exception exception)
{
MessageBox.Show ("Unexpected exception occured: " + exception.Messa ge);
}
//First we bind the parent records
cboOID.DataSour ce = DBDataSet;
cboOID.DisplayM ember = "Person.OID ";
cboOID.ValueMem ber = "Person.OID ";
txtAddress.Data Bindings.Add("T ext",DBDataSet, "Person.address ");
txtBalance.Data Bindings.Add("T ext",DBDataSet, "Person.balance ");
txtId.DataBindi ngs.Add("Text", DBDataSet,"Pers on.identificati on");
txtLastName.Dat aBindings.Add(" Text",DBDataSet ,"Person.lastNa me");
txtName.DataBin dings.Add("Text ",DBDataSet,"Pe rson.name");
txtTimeStamp.Da taBindings.Add( "Text",DBDataSe t,"Person.timeS tamp");
grdCourses.SetD ataBinding(DBDa taSet,"Person.P ersonCrossTable ");
InitGridStyles( grdCourses);
AutoSizeTable(g rdCourses);
DisableSelected IndexChanged = false;
}
private void cboOID_Selected IndexChanged(ob ject sender, System.EventArg s e)
{
if (!DisableSelect edIndexChanged)
AutoSizeTable(g rdCourses);
}