seeking solution for DataGrid, help, help, help, help

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();
}
}
 
F

Frank Oquendo

pei_world said:
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?

Yes, you're right. The solution is to separate the task of setting up
your grid from filling the grid. I'd be inclined to eastablish my grid
columns and styles during the form's Load event.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
P

pei_world

but i need to set up each datagrid column style differently. that means I
have about 6 functions like that each with no common settings.
so How can I do it?
 
F

Frank Oquendo

pei_world said:
Additional information: The 'NEWS ID' DataGridColumnStyle cannot be
used because it is not associated with a Property or Column in the
DataSource.

You're using a single grid to display data from different tables and
those tables have differing schemas, right?

If so, modify the MappingName property of your columns to match the
names of the columns in your data source.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
P

pei_world

I have the following function defined to new a new DataGridTableStyle, and
map the mappingName to tablename, but it still doesn't work.
anyone know how to do it?


private DataTable FillDataTable(DataGrid dg,string[] DGCN,DataTable dt)
{
DataColumn dc; //column of table
foreach(string cn in DGCN)
{
dc = new DataColumn(cn);
dc.DataType = System.Type.GetType("System.String");
dc.DefaultValue="";
dt.Columns.Add(dc);
}
//
// dataGridTableStyle1
//
DataGridTableStyle dgts = new DataGridTableStyle();
dgts.MappingName = dt.TableName;
dg.TableStyles.Add(dgts);
dgts.AlternatingBackColor =
System.Drawing.SystemColors.InactiveCaptionText;
dgts.DataGrid = dg;
dgts.ForeColor = System.Drawing.SystemColors.ControlText;
dgts.GridLineColor = System.Drawing.SystemColors.ControlDarkDark;
dgts.HeaderBackColor = System.Drawing.SystemColors.InactiveCaption;
dgts.HeaderFont = new System.Drawing.Font("Tahoma", 9F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
((System.Byte)(0)));
dgts.HeaderForeColor = System.Drawing.SystemColors.Info;
dgts.MappingName = "";
dgts.ReadOnly = true;

return dt;
}
 
F

Frank Oquendo

pei_world said:
I have the following function defined to new a new
DataGridTableStyle, and map the mappingName to tablename, but it
still doesn't work.

Towards the end, you end up clearing the MappingName for the style.
Remove that line.
dgts.MappingName = dt.TableName;
dgts.MappingName = "";

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
P

pei_world

same error as before,not solving the problem

//==================================================================
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.
//==================================================================
 
P

pei_world

I have solved the problem by having a new function to create new
DataGridTableColumnStyle
 
F

Frank Oquendo

pei_world said:
same error as before,not solving the problem

The problem's in the code you don't show. Zip the project sources and
attach them to your next reply.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top