473,738 Members | 10,643 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding a column to bound datagrid

Dear NG,

After being away from C# programming for a spell, I am trying my hand at
what should be a simple task. I have been hitting my head against the wall
this morning. I have a simple order entry application. The code below gets
line items from a SQL Server database and returns them to a datagrid by way
of a DataTable called lineTable. As long as I am just displaying columns in
the SQL database everything works well. I now want to display a 12th column
computed as extended price. That column is NOT in the table but is computed
by the line: extPrice = Convert.ToDecim al(dr[3]) * Convert.ToDecim al(dr[7]);
As long as I display it in a MessageBox all is well. I have defined a
column 12 using the Collection Editor. I had hoped that by setting the
MappingName property to extPrice I would be home free, but no such luck.
Any help would be appreciated.

Sincerely,

Robert Schuldenfrei (bo*@s-i-inc.com)

private void btnGet_Click(ob ject sender, System.EventArg s e)

{

decimal extPrice;

coHeader = CustomerOrderTb l.GetCoHeader(t xtOrderNo.Text) ;

if (coHeader == null)

{

MessageBox.Show ("Invalid customer order number.", "Entry error");

}

else

{

ShowData(); //display header

//display line items in datagrid

DataTable lineTable = CustomerOrderTb l.GetLines(txtO rderNo.Text);

// Instead of a MessageBox display, I want the extended price to

// appear in column 12 of the data grid. Column [3] is quantity.

// Column [7] is unit price.
foreach (DataRow dr in lineTable.Rows)

{

extPrice = Convert.ToDecim al(dr[3]) * Convert.ToDecim al(dr[7]);

MessageBox.Show (extPrice.ToStr ing());

}

grdLine.DataSou rce = lineTable;

}

} //end btnGet_Click()
Nov 17 '05 #1
6 3247
Robert,

You cannot use C# code in a datatable column.
However you can add a column with an expression. I did not check your
expression however maybe can you try that yourself.

http://msdn.microsoft.com/library/de...ssiontopic.asp

I hope this helps,

Cor

"Robert Schuldenfrei" <sc***********@ bellsouth.net> schreef in bericht
news:X1******** *******@bignews 4.bellsouth.net ...
Dear NG,

After being away from C# programming for a spell, I am trying my hand at
what should be a simple task. I have been hitting my head against the
wall this morning. I have a simple order entry application. The code
below gets line items from a SQL Server database and returns them to a
datagrid by way of a DataTable called lineTable. As long as I am just
displaying columns in the SQL database everything works well. I now want
to display a 12th column computed as extended price. That column is NOT
in the table but is computed by the line: extPrice =
Convert.ToDecim al(dr[3]) * Convert.ToDecim al(dr[7]); As long as I display
it in a MessageBox all is well. I have defined a column 12 using the
Collection Editor. I had hoped that by setting the MappingName property
to extPrice I would be home free, but no such luck. Any help would be
appreciated.

Sincerely,

Robert Schuldenfrei (bo*@s-i-inc.com)

private void btnGet_Click(ob ject sender, System.EventArg s e)

{

decimal extPrice;

coHeader = CustomerOrderTb l.GetCoHeader(t xtOrderNo.Text) ;

if (coHeader == null)

{

MessageBox.Show ("Invalid customer order number.", "Entry error");

}

else

{

ShowData(); //display header

//display line items in datagrid

DataTable lineTable = CustomerOrderTb l.GetLines(txtO rderNo.Text);

// Instead of a MessageBox display, I want the extended price to

// appear in column 12 of the data grid. Column [3] is quantity.

// Column [7] is unit price.
foreach (DataRow dr in lineTable.Rows)

{

extPrice = Convert.ToDecim al(dr[3]) * Convert.ToDecim al(dr[7]);

MessageBox.Show (extPrice.ToStr ing());

}

grdLine.DataSou rce = lineTable;

}

} //end btnGet_Click()

Nov 17 '05 #2
Kav

"Robert Schuldenfrei" <sc***********@ bellsouth.net> wrote in message
news:X1******** *******@bignews 4.bellsouth.net ...
Dear NG,

After being away from C# programming for a spell, I am trying my hand at
what should be a simple task. I have been hitting my head against the
wall this morning. I have a simple order entry application. The code
below gets line items from a SQL Server database and returns them to a
datagrid by way of a DataTable called lineTable. As long as I am just
displaying columns in the SQL database everything works well. I now want
to display a 12th column computed as extended price. That column is NOT
in the table but is computed by the line: extPrice =
Convert.ToDecim al(dr[3]) * Convert.ToDecim al(dr[7]); As long as I display
it in a MessageBox all is well. I have defined a column 12 using the
Collection Editor. I had hoped that by setting the MappingName property
to extPrice I would be home free, but no such luck. Any help would be
appreciated.

Sincerely,

Robert Schuldenfrei (bo*@s-i-inc.com)

private void btnGet_Click(ob ject sender, System.EventArg s e)

{

decimal extPrice;

coHeader = CustomerOrderTb l.GetCoHeader(t xtOrderNo.Text) ;

if (coHeader == null)

{

MessageBox.Show ("Invalid customer order number.", "Entry error");

}

else

{

ShowData(); //display header

//display line items in datagrid

DataTable lineTable = CustomerOrderTb l.GetLines(txtO rderNo.Text);

// Instead of a MessageBox display, I want the extended price to

// appear in column 12 of the data grid. Column [3] is quantity.

// Column [7] is unit price.
foreach (DataRow dr in lineTable.Rows)

{

extPrice = Convert.ToDecim al(dr[3]) * Convert.ToDecim al(dr[7]);

MessageBox.Show (extPrice.ToStr ing());

}

grdLine.DataSou rce = lineTable;

}

} //end btnGet_Click()


Hi Robert,

Wouldn't it be easier to add the column to the Table calculate the values
and then bind it?

public void addColumn(strin g name, string type)
{
DataColumn dc = new DataColumn();
dc.DataType = Type.GetType(ty pe);
dc.ColumnName = name;
this.datatable. Columns.Add(dc) ;
}

Then foreach row in the table calculate the total and bind it?

Rich.
Nov 17 '05 #3
> Hi Robert,

Wouldn't it be easier to add the column to the Table calculate the values
and then bind it?

public void addColumn(strin g name, string type)
{
DataColumn dc = new DataColumn();
dc.DataType = Type.GetType(ty pe);
dc.ColumnName = name;
this.datatable. Columns.Add(dc) ;
}

Then foreach row in the table calculate the total and bind it?

Rich.


Hi Rich,

I think your suggestion was right on target. The code below was written as
per your idea. It executes correctly and the MessageBox reports the correct
values for dr[11]. However, my datagrid only shows the columns from the
unexpanded table (columns 0-10). I used the Collection Editor to give
column 11 a name, but I had no idea what to place in the MappingName so I
left it blank. Is that my problem? In all of the other columns I gave the
MappingName the name of the original SQL column name. For example column 4
(datagrid columns start at 1, not 0) has a MappingName of co_li_QtyOrdere d.
This is the DataTable column dr[3].

Thanks in advance,

Bob (bo*@s-i-inc.com)

private void btnGet_Click(ob ject sender, System.EventArg s e)

{

coHeader = CustomerOrderTb l.GetCoHeader(t xtOrderNo.Text) ;

if (coHeader == null)

{

MessageBox.Show ("Invalid customer order number.", "Entry error");

}

else

{

ShowData(); //display header

//display line items in datagrid

DataTable lineTable = CustomerOrderTb l.GetLines(txtO rderNo.Text);

// Instead of a MessageBox display, I want the extended price to

// appear in column 12 of the data grid. Column [3] is quantity.

// Column [7] is unit price.
DataColumn dc = new DataColumn();

dc.DataType = Type.GetType("S ystem.Decimal") ;

lineTable.Colum ns.Add(dc);

dc.ColumnName = "Ext. Price";

foreach (DataRow dr in lineTable.Rows)

{

dr[11] = Convert.ToDecim al(dr[3]) * Convert.ToDecim al(dr[7]);

MessageBox.Show (dr[11].ToString());

}

grdLine.DataSou rce = lineTable;

}

} //end btnGet_Click()
Nov 17 '05 #4
Yes. You'll need to give the column a name and add a DataGridTextBox Column
with a mapping name that matches to the DataGridTableSt yles object your grid
is using.

If you create DataGridColumnS tyles for the columns, then the grid will only
display the columns that have matching styles.

Pete

"Robert Schuldenfrei" <sc***********@ bellsouth.net> wrote in message
news:5l******** *********@bigne ws3.bellsouth.n et...
Hi Robert,

Wouldn't it be easier to add the column to the Table calculate the values
and then bind it?

public void addColumn(strin g name, string type)
{
DataColumn dc = new DataColumn();
dc.DataType = Type.GetType(ty pe);
dc.ColumnName = name;
this.datatable. Columns.Add(dc) ;
}

Then foreach row in the table calculate the total and bind it?

Rich.


Hi Rich,

I think your suggestion was right on target. The code below was written
as per your idea. It executes correctly and the MessageBox reports the
correct values for dr[11]. However, my datagrid only shows the columns
from the unexpanded table (columns 0-10). I used the Collection Editor to
give column 11 a name, but I had no idea what to place in the MappingName
so I left it blank. Is that my problem? In all of the other columns I
gave the MappingName the name of the original SQL column name. For
example column 4 (datagrid columns start at 1, not 0) has a MappingName of
co_li_QtyOrdere d. This is the DataTable column dr[3].

Thanks in advance,

Bob (bo*@s-i-inc.com)

private void btnGet_Click(ob ject sender, System.EventArg s e)

{

coHeader = CustomerOrderTb l.GetCoHeader(t xtOrderNo.Text) ;

if (coHeader == null)

{

MessageBox.Show ("Invalid customer order number.", "Entry error");

}

else

{

ShowData(); //display header

//display line items in datagrid

DataTable lineTable = CustomerOrderTb l.GetLines(txtO rderNo.Text);

// Instead of a MessageBox display, I want the extended price to

// appear in column 12 of the data grid. Column [3] is quantity.

// Column [7] is unit price.
DataColumn dc = new DataColumn();

dc.DataType = Type.GetType("S ystem.Decimal") ;

lineTable.Colum ns.Add(dc);

dc.ColumnName = "Ext. Price";

foreach (DataRow dr in lineTable.Rows)

{

dr[11] = Convert.ToDecim al(dr[3]) * Convert.ToDecim al(dr[7]);

MessageBox.Show (dr[11].ToString());

}

grdLine.DataSou rce = lineTable;

}

} //end btnGet_Click()

Nov 17 '05 #5
Hi Pete,

Thanks for the information. I am having trouble following your
instructions. Exactly WHAT column name goes into into the MappingName
property? For all of the columns that I see correctly, I have used the SQL
column names in the MappingName property. For example: column 4 (datagrid
columns start at 1, not 0) has a MappingName of co_li_QtyOrdere d. This is
the DataTable column dr[3]. Since the new column does not exist in SQL I do
not know what goes in this field. If I leave it blank, as you point out, no
column appears in the datagrid.

Thanks in advance,

Bob (bo*@s-i-inc.com)

"Pete Davis" <pdavis68@[nospam]hotmail.com> wrote in message
news:FY******** ************@gi ganews.com...
Yes. You'll need to give the column a name and add a DataGridTextBox Column
with a mapping name that matches to the DataGridTableSt yles object your
grid is using.

If you create DataGridColumnS tyles for the columns, then the grid will
only display the columns that have matching styles.

Pete

"Robert Schuldenfrei" <sc***********@ bellsouth.net> wrote in message
news:5l******** *********@bigne ws3.bellsouth.n et...
Hi Robert,

Wouldn't it be easier to add the column to the Table calculate the
values and then bind it?

public void addColumn(strin g name, string type)
{
DataColumn dc = new DataColumn();
dc.DataType = Type.GetType(ty pe);
dc.ColumnName = name;
this.datatable. Columns.Add(dc) ;
}

Then foreach row in the table calculate the total and bind it?

Rich.


Hi Rich,

I think your suggestion was right on target. The code below was written
as per your idea. It executes correctly and the MessageBox reports the
correct values for dr[11]. However, my datagrid only shows the columns
from the unexpanded table (columns 0-10). I used the Collection Editor
to give column 11 a name, but I had no idea what to place in the
MappingName so I left it blank. Is that my problem? In all of the other
columns I gave the MappingName the name of the original SQL column name.
For example column 4 (datagrid columns start at 1, not 0) has a
MappingName of co_li_QtyOrdere d. This is the DataTable column dr[3].

Thanks in advance,

Bob (bo*@s-i-inc.com)

private void btnGet_Click(ob ject sender, System.EventArg s e)

{

coHeader = CustomerOrderTb l.GetCoHeader(t xtOrderNo.Text) ;

if (coHeader == null)

{

MessageBox.Show ("Invalid customer order number.", "Entry error");

}

else

{

ShowData(); //display header

//display line items in datagrid

DataTable lineTable = CustomerOrderTb l.GetLines(txtO rderNo.Text);

// Instead of a MessageBox display, I want the extended price to

// appear in column 12 of the data grid. Column [3] is quantity.

// Column [7] is unit price.
DataColumn dc = new DataColumn();

dc.DataType = Type.GetType("S ystem.Decimal") ;

lineTable.Colum ns.Add(dc);

dc.ColumnName = "Ext. Price";

foreach (DataRow dr in lineTable.Rows)

{

dr[11] = Convert.ToDecim al(dr[3]) * Convert.ToDecim al(dr[7]);

MessageBox.Show (dr[11].ToString());

}

grdLine.DataSou rce = lineTable;

}

} //end btnGet_Click()


Nov 17 '05 #6
Dear NG Readers,

I have no idea if anyone follows up on a thread this old, but thanks to
Pete, Cor, Kav, and my own poking around I have solved this problem. The
key concept that I failed to grasp is that I should do all of the work in
code and stay away from the Collections Editor. Establish the DataGrid in
the form's design and do as little as possible in the design's property
sheet. Here is the code that worked:

private void btnGet_Click(ob ject sender, System.EventArg s e)

{

decimal extPrice;

coHeader = CustomerOrderTb l.GetCoHeader(t xtOrderNo.Text) ; //Load header

if (coHeader == null)

{

MessageBox.Show ("Invalid customer order number.", "Entry error");

}

else

{

ShowData(); //display header

//Load line items into DataTable from SQL database

DataTable lineTable = CustomerOrderTb l.GetLines(txtO rderNo.Text);

//Label up the column headings

lineTable.Colum ns[0].ColumnName = "Order";

lineTable.Colum ns[1].ColumnName = "Seq.";

lineTable.Colum ns[2].ColumnName = "Part No.";

lineTable.Colum ns[3].ColumnName = "Ordered";

lineTable.Colum ns[4].ColumnName = "To Ship";

lineTable.Colum ns[5].ColumnName = "Shipped";

lineTable.Colum ns[6].ColumnName = "P/C";

lineTable.Colum ns[7].ColumnName = "Price";

lineTable.Colum ns[8].ColumnName = "Due Date";

lineTable.Colum ns[9].ColumnName = "Note";

lineTable.Colum ns[10].ColumnName = "Serial No.";

DataColumn dc = new DataColumn(); //New column for ext. price

dc.DataType = Type.GetType("S ystem.Decimal") ;

lineTable.Colum ns.Add(dc);

dc.ColumnName = "Ext. Price";

foreach (DataRow dr in lineTable.Rows) //Form new column with ext. price

{

extPrice = Convert.ToDecim al(dr[3]) * Convert.ToDecim al(dr[7]);

dr[11] = extPrice;

}

grdLine.SetData Binding(lineTab le, ""); //Bind DataTable to DataGrid

}

} //end btnGet_Click()

Robert Schuldenfrei (bo*@s-i-inc.com)
"Pete Davis" <pdavis68@[nospam]hotmail.com> wrote in message
news:FY******** ************@gi ganews.com...
Yes. You'll need to give the column a name and add a DataGridTextBox Column
with a mapping name that matches to the DataGridTableSt yles object your
grid is using.

If you create DataGridColumnS tyles for the columns, then the grid will
only display the columns that have matching styles.

Pete


Nov 17 '05 #7

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

Similar topics

0
2685
by: Brian Greiwe | last post by:
I posted this in the datagrid forum but got no bites, so I thought I'd post it here as well for some help.... I've created a datagrid with 1 edititemtemplate column. When the user clicks edit, it spans the entire width of the datagrid (removing the other cells). This column has several controls in it (text boxes, radiobuttons, and a dropdownlist). Also, on the itemdatabound, I am adding another row to the datagrid (above the...
3
4880
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that the best method? Do you have a sample of how to do this?
1
4235
by: sianan | last post by:
I tried to use the following example, to add a checkbox column to a DataGrid in an ASP.NET application: http://www.codeproject.com/aspnet/datagridcheckbox.asp For some reason, I simply CAN'T get the example to work. I created the following two classes, provided with the example: *-*-**-*-*-*-*-*-*-*-*-*-**-*-*-*-*-CheckBoxColumn Class:-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-**-*-*-*
5
4662
by: Aaron Ackerman | last post by:
I have a bound combobox the appears on a cell within the column of my bound grid when the user clicks on a cell n(In my vb.net WinForm app). I am trying to allow the adding of an item to that bound combo by allowing the user to dynamically type in the new value directly in the combo box whcih in turn updates the lookup table and then when they have finished leaves that particular cell on the grid with the correct value. Seems...
2
2313
by: Mike Fellows | last post by:
I want to add a fresh column to a datagrid that is bound to a datasource i want it to be the first column on the datagrid so that i can number each row 1,2,3,4.... etc. (but i have no need to store this within my database) how do i add an empty column? Thanks
4
2454
by: Grace | last post by:
I use a datagrid included in a repeater. as following: (.aspx) <asp:repeater id="rp_perform" Runat="server"> <HeaderTemplate> <table border="0" bgcolor="gray" cellspacing="0" cellpadding="0"> </HeaderTemplate> <ItemTemplate> <tr bgcolor="white"> <td width="100%">
4
2917
by: gane | last post by:
Hi, I am creating datagrid bound column dynamically and need to check if a datagrid column already exists?Is there a way to check this? thanks gane
0
1391
by: James | last post by:
I have a source excel file which I'm reading into a dataset. This works just fine, the end user picks which sheet they want and I can access the data. But I'm trying to integrate a "First Row Contains Column Headings" element. When it's checked, I have no problems. I just bind and have it generate the columns automatically. However, when it's not checked, I'd like it to just add Column0, Column1, etc. etc. for each column in the...
1
2247
by: RSH | last post by:
Hi, I have a datagrid that is bound to a datasource. I now need to add a checkbox column to the datagrid in column(0) position which is independent of the datasource. I will be checking for the checked checkboxes on postback and writing the ID column value to a table. I need to add the checkbox column in code. How do i go about doing this?
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9476
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.