473,324 Members | 2,370 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,324 software developers and data experts.

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.ToDecimal(dr[3]) * Convert.ToDecimal(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(object sender, System.EventArgs e)

{

decimal extPrice;

coHeader = CustomerOrderTbl.GetCoHeader(txtOrderNo.Text);

if (coHeader == null)

{

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

}

else

{

ShowData(); //display header

//display line items in datagrid

DataTable lineTable = CustomerOrderTbl.GetLines(txtOrderNo.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.ToDecimal(dr[3]) * Convert.ToDecimal(dr[7]);

MessageBox.Show(extPrice.ToString());

}

grdLine.DataSource = lineTable;

}

} //end btnGet_Click()
Nov 17 '05 #1
6 3209
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***************@bignews4.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.ToDecimal(dr[3]) * Convert.ToDecimal(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(object sender, System.EventArgs e)

{

decimal extPrice;

coHeader = CustomerOrderTbl.GetCoHeader(txtOrderNo.Text);

if (coHeader == null)

{

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

}

else

{

ShowData(); //display header

//display line items in datagrid

DataTable lineTable = CustomerOrderTbl.GetLines(txtOrderNo.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.ToDecimal(dr[3]) * Convert.ToDecimal(dr[7]);

MessageBox.Show(extPrice.ToString());

}

grdLine.DataSource = lineTable;

}

} //end btnGet_Click()

Nov 17 '05 #2
Kav

"Robert Schuldenfrei" <sc***********@bellsouth.net> wrote in message
news:X1***************@bignews4.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.ToDecimal(dr[3]) * Convert.ToDecimal(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(object sender, System.EventArgs e)

{

decimal extPrice;

coHeader = CustomerOrderTbl.GetCoHeader(txtOrderNo.Text);

if (coHeader == null)

{

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

}

else

{

ShowData(); //display header

//display line items in datagrid

DataTable lineTable = CustomerOrderTbl.GetLines(txtOrderNo.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.ToDecimal(dr[3]) * Convert.ToDecimal(dr[7]);

MessageBox.Show(extPrice.ToString());

}

grdLine.DataSource = 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(string name, string type)
{
DataColumn dc = new DataColumn();
dc.DataType = Type.GetType(type);
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(string name, string type)
{
DataColumn dc = new DataColumn();
dc.DataType = Type.GetType(type);
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_QtyOrdered.
This is the DataTable column dr[3].

Thanks in advance,

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

private void btnGet_Click(object sender, System.EventArgs e)

{

coHeader = CustomerOrderTbl.GetCoHeader(txtOrderNo.Text);

if (coHeader == null)

{

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

}

else

{

ShowData(); //display header

//display line items in datagrid

DataTable lineTable = CustomerOrderTbl.GetLines(txtOrderNo.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("System.Decimal");

lineTable.Columns.Add(dc);

dc.ColumnName = "Ext. Price";

foreach (DataRow dr in lineTable.Rows)

{

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

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

}

grdLine.DataSource = lineTable;

}

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

If you create DataGridColumnStyles 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*****************@bignews3.bellsouth.net...
Hi Robert,

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

public void addColumn(string name, string type)
{
DataColumn dc = new DataColumn();
dc.DataType = Type.GetType(type);
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_QtyOrdered. This is the DataTable column dr[3].

Thanks in advance,

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

private void btnGet_Click(object sender, System.EventArgs e)

{

coHeader = CustomerOrderTbl.GetCoHeader(txtOrderNo.Text);

if (coHeader == null)

{

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

}

else

{

ShowData(); //display header

//display line items in datagrid

DataTable lineTable = CustomerOrderTbl.GetLines(txtOrderNo.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("System.Decimal");

lineTable.Columns.Add(dc);

dc.ColumnName = "Ext. Price";

foreach (DataRow dr in lineTable.Rows)

{

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

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

}

grdLine.DataSource = 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_QtyOrdered. 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********************@giganews.com...
Yes. You'll need to give the column a name and add a DataGridTextBoxColumn
with a mapping name that matches to the DataGridTableStyles object your
grid is using.

If you create DataGridColumnStyles 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*****************@bignews3.bellsouth.net...
Hi Robert,

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

public void addColumn(string name, string type)
{
DataColumn dc = new DataColumn();
dc.DataType = Type.GetType(type);
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_QtyOrdered. This is the DataTable column dr[3].

Thanks in advance,

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

private void btnGet_Click(object sender, System.EventArgs e)

{

coHeader = CustomerOrderTbl.GetCoHeader(txtOrderNo.Text);

if (coHeader == null)

{

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

}

else

{

ShowData(); //display header

//display line items in datagrid

DataTable lineTable = CustomerOrderTbl.GetLines(txtOrderNo.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("System.Decimal");

lineTable.Columns.Add(dc);

dc.ColumnName = "Ext. Price";

foreach (DataRow dr in lineTable.Rows)

{

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

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

}

grdLine.DataSource = 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(object sender, System.EventArgs e)

{

decimal extPrice;

coHeader = CustomerOrderTbl.GetCoHeader(txtOrderNo.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 = CustomerOrderTbl.GetLines(txtOrderNo.Text);

//Label up the column headings

lineTable.Columns[0].ColumnName = "Order";

lineTable.Columns[1].ColumnName = "Seq.";

lineTable.Columns[2].ColumnName = "Part No.";

lineTable.Columns[3].ColumnName = "Ordered";

lineTable.Columns[4].ColumnName = "To Ship";

lineTable.Columns[5].ColumnName = "Shipped";

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

lineTable.Columns[7].ColumnName = "Price";

lineTable.Columns[8].ColumnName = "Due Date";

lineTable.Columns[9].ColumnName = "Note";

lineTable.Columns[10].ColumnName = "Serial No.";

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

dc.DataType = Type.GetType("System.Decimal");

lineTable.Columns.Add(dc);

dc.ColumnName = "Ext. Price";

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

{

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

dr[11] = extPrice;

}

grdLine.SetDataBinding(lineTable, ""); //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********************@giganews.com...
Yes. You'll need to give the column a name and add a DataGridTextBoxColumn
with a mapping name that matches to the DataGridTableStyles object your
grid is using.

If you create DataGridColumnStyles 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
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...
3
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...
1
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...
5
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...
2
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...
4
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">...
4
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
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...
1
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.