Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Data Binding / March 2005

Tip: Looking for answers? Try searching our database.

How to display lookup field in DataGrid

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Kamal Ahmed - 04 Mar 2005 06:24 GMT
Hi, All

I have a datagrid binded to a datasource. I want to display a lookup field
in the same grid. I also want to display a calculated field in datagrid i.e.
Qty x Rate (Amount). How is it possible.

TIA
Kamal Ahmed - 15 Mar 2005 07:13 GMT
Anybody does know about my problem. ???

> Hi, All
>
[quoted text clipped - 4 lines]
>
> TIA
Andrew and Maree Cassidy - 15 Mar 2005 22:41 GMT
Kamal,

I am new to C#, and cannot help with the lookup field, but I have added a
calculated field to my datagrid - it is not too difficult.

You need to manually define the DataGridTableStyle and set up the columns
using DataGridColumnStyle.  The other advantage of this is you can define
alternate column headings and column widths as well.

Here is a section of my code that sets up the columns for one of my
datagrids... including a calculated column.  The Expression is where you
perform the calculation (ie "Qty * Amount) - you simply use field names. In
my case I calculate a value from a child table.

DataGridTableStyle ts = new DataGridTableStyle();
DataGridColumnStyle csCustID = new DataGridTextBoxColumn();
DataGridColumnStyle csSurname = new DataGridTextBoxColumn();
DataGridColumnStyle csFirstName = new DataGridTextBoxColumn();
DataGridColumnStyle csAddress = new DataGridTextBoxColumn();
DataGridColumnStyle csLastServ = new DataGridTextBoxColumn();

private void setupCustomerColumns()
{
   // Set the DataGridTableStyle.MappingName property
   // to the table in the data source to map to.
   ts.MappingName = dsCRSdata.Tables[0].TableName;

   // Set up the header text and display length for each column
   setupColumn(0, csCustID, 0,"ID", 20);
   setupColumn(0, csSurname, 1,"", 100);
   setupColumn(0, csFirstName, 2,"First Name", 100);
   setupColumn(0, csAddress, 3, "",200);

   dsCRSdata.Tables[0].Columns.Add("LastServiceDate", typeof(DateTime));
   dsCRSdata.Tables[0].Columns["LastServiceDate"].Expression = "max
"Child.ServiceDate)";

   setupColumn(0, csLastServ, 4, "Last Service Date",80);

   // Add columns in display order (not necessarily database order)
   ts.GridColumnStyles.Add(csCustID);
   ts.GridColumnStyles.Add(csFirstName);
   ts.GridColumnStyles.Add(csSurname);
   ts.GridColumnStyles.Add(csAddress);
   ts.GridColumnStyles.Add(csLastServ);

   // Add it to the datagrid's TableStyles collection
   dgCustList.TableStyles.Add(ts);
}

private void setupColumn(int table, DataGridColumnStyle col, int colNo,
String colName, int width)
{
   col.MappingName = col.HeaderText =
dsCRSdata.Tables[table].Columns[colNo].ColumnName;

   if (colName == "")
   {
       col.HeaderText = dsCRSdata.Tables[table].Columns[colNo].ColumnName;
   }
   else
   {
       col.HeaderText = colName;
   }

   col.Width = width;
}

Hope that helps

Maree
> Anybody does know about my problem. ???
>
[quoted text clipped - 7 lines]
>>
>> TIA

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.