P
pei_world
Hi, there;
I have a problem with my datagrid control. I declared it in one of my
form, set with DataGridTalbeStyle as well, and when I click on button, I
would like to retrive Data from Database and bind these data to my datagrid
in the form,
when I click the button first time, it work fine. but when I click the
second button. it report belowing error, I guess it is because it already
set up the column name at first time, so cann't do it on the second time. am
I right?
anyone know how to solve it, I really need help on these.
thanks in advanced.
//========================= Error Details =================================
An unhandled exception of type 'System.InvalidOperationException' occurred
in system.windows.forms.dll
Additional information: The 'NEWS ID' DataGridColumnStyle cannot be used
because it is not associated with a Property or Column in the DataSource.
//==========================================================================
====
//=======================================================================
private void FillDataGrid_News(DataGrid dg,SqlConnection conn,DataSet
ds,string TN)
{
//table name "news"
string SelectCommand = "SELECT * FROM "+TN;
//columns title name
string[] DataGridColumnName = new string[]{"NEWS
ID","TITLE","CONTENT","AUTHOR","DATE","PUB UNTIL"};
DataTable dt= new DataTable(); // datatable for grid
//set data source
dg.DataSource = FillDataTable(DataGridColumnName,dt);
//setting the width
GridColumnStylesCollection colStyle;
colStyle = dg.TableStyles[0].GridColumnStyles;
colStyle[0].Width = 75; // News ID //<<<<<<<<<<<<<<<<<<<<<<Error
point
colStyle[1].Width = 150; // Title
colStyle[2].Width = 250; // Content
colStyle[3].Width = 120; // Author
colStyle[4].Width = 70; // Date
colStyle[5].Width = 70; // Publish Until
//retrive data to the DataSet
ds = retriveDS(conn,SelectCommand,ds,TN);
//fill data to datagrid Row=i, Column=j
for(int i=0;i<ds.Tables[TN].Rows.Count;i++)
{
dt.LoadDataRow(DataGridColumnName,true);
for(int j=0;j<ds.Tables[TN].Columns.Count;j++)
dg[i,j] = ds.Tables[TN].Rows.ItemArray[j].ToString();
}
}
I have a problem with my datagrid control. I declared it in one of my
form, set with DataGridTalbeStyle as well, and when I click on button, I
would like to retrive Data from Database and bind these data to my datagrid
in the form,
when I click the button first time, it work fine. but when I click the
second button. it report belowing error, I guess it is because it already
set up the column name at first time, so cann't do it on the second time. am
I right?
anyone know how to solve it, I really need help on these.
thanks in advanced.
//========================= Error Details =================================
An unhandled exception of type 'System.InvalidOperationException' occurred
in system.windows.forms.dll
Additional information: The 'NEWS ID' DataGridColumnStyle cannot be used
because it is not associated with a Property or Column in the DataSource.
//==========================================================================
====
//=======================================================================
private void FillDataGrid_News(DataGrid dg,SqlConnection conn,DataSet
ds,string TN)
{
//table name "news"
string SelectCommand = "SELECT * FROM "+TN;
//columns title name
string[] DataGridColumnName = new string[]{"NEWS
ID","TITLE","CONTENT","AUTHOR","DATE","PUB UNTIL"};
DataTable dt= new DataTable(); // datatable for grid
//set data source
dg.DataSource = FillDataTable(DataGridColumnName,dt);
//setting the width
GridColumnStylesCollection colStyle;
colStyle = dg.TableStyles[0].GridColumnStyles;
colStyle[0].Width = 75; // News ID //<<<<<<<<<<<<<<<<<<<<<<Error
point
colStyle[1].Width = 150; // Title
colStyle[2].Width = 250; // Content
colStyle[3].Width = 120; // Author
colStyle[4].Width = 70; // Date
colStyle[5].Width = 70; // Publish Until
//retrive data to the DataSet
ds = retriveDS(conn,SelectCommand,ds,TN);
//fill data to datagrid Row=i, Column=j
for(int i=0;i<ds.Tables[TN].Rows.Count;i++)
{
dt.LoadDataRow(DataGridColumnName,true);
for(int j=0;j<ds.Tables[TN].Columns.Count;j++)
dg[i,j] = ds.Tables[TN].Rows.ItemArray[j].ToString();
}
}