473,407 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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

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[i].ItemArray[j].ToString();
}
}
Nov 15 '05 #1
8 3113
pei_world wrote:
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)
Nov 15 '05 #2
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?
"pei_world" <pe*******@hotmail.com> wrote in message
news:eG**************@TK2MSFTNGP12.phx.gbl...
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[i].ItemArray[j].ToString();
}
}

Nov 15 '05 #3
pei_world wrote:
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)
Nov 15 '05 #4
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;
}

"Frank Oquendo" <fr****@acadxpin.com> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
pei_world wrote:
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)

Nov 15 '05 #5
pei_world wrote:
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; <snip> 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)
Nov 15 '05 #6

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

"pei_world" <pe*******@hotmail.com> wrote in message
news:eG**************@TK2MSFTNGP12.phx.gbl...
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[i].ItemArray[j].ToString();
}
}

Nov 15 '05 #7
I have solved the problem by having a new function to create new
DataGridTableColumnStyle

"pei_world" <pe*******@hotmail.com> wrote in message
news:eG**************@TK2MSFTNGP12.phx.gbl...
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[i].ItemArray[j].ToString();
}
}

Nov 15 '05 #8
pei_world wrote:
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)
Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bernhard | last post by:
Hi, Presently I am writing a programm to display the status of phone extensions. For this purpose I use a WinForm application. The status of the lines with the respective user name should be...
4
by: ben | last post by:
Hi All, I have a table in the database with one column of bit datatype...how can i retrieve its value as yes/no or true/false in my datagrid column, i realize i need to convert it in the query n...
3
by: Coleen | last post by:
Hi All :-) No one has posted with any suggestions on how to get a grand total or computed column value back into a usable variable to perform percentage calculation on the same column you are...
0
by: John Wells | last post by:
My company is ramping up for a long ERP conversion project from Progress RDBMS and 4GL (www.progress.com) to a Postgresql/Java solution. I'm seeking an *expert* consultant(s) to establish an...
10
by: Susan Baker | last post by:
Hi Guys (and girls), I am in a bit of a bind. I'm looking for a simple "proof of concept" C# app (WinForm OR console) that sends a web request to a simple PHP script and receives BINARY data...
29
by: Jim | last post by:
I want to extract data from several websites that I visit daily. I'd like to condense the info into a single web page that I can visit (instead of the multiple websites I have to visit now to get...
3
by: Thom Anderson | last post by:
Hi. I have been asked to create a web tool for my company. Basically an employee will use a user control (a tree view containing information about our company) and when they get to where they...
7
by: semedao | last post by:
Hi, I am using cryptostream on both sides on tcp connection that pass data. I am also use asyc socket , so , the data that recieved in the callback method not always have the length of the buffer...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.