473,467 Members | 1,300 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to bind columns in a datagrid

147 New Member
Hi

I have a datagridview and it correctly displays the results according to the user input of a start date and end date.

I use the datagridview1.autogeneratecolumns = true to show all the fields.

I now need to show only specific fields but dont' want to edit the SQL.

I can add a column easily enough using datagridview1.Coumns.Add("colnName", "colHeader");

How do I bind data to 'colName'

If it helps, my code:

Expand|Select|Wrap|Line Numbers
  1.         public void OpenDatabase(DateTime ddtpFrom, DateTime ddtpTo)
  2.         {
  3.  
  4.             SqlConnection conn = new SqlConnection("Data Source=SBSHK; Initial Catalog=123; User ID=xxx; Password=xxx;");
  5.  
  6.             string strSQL = @"SELECT TOP (100) Glcode AS 代碼, Gldesc AS 描述, [Entry Number] AS 詞條數字, [Company Name] AS 公司名称, Mark AS 標記, Debit AS 借方, Credit AS 信用 FROM View_TransactionReport WHERE DATE BETWEEN @f AND @t AND dagbknr IS NOT NULL AND transtype <> 'V' AND (Debit IS NOT NULL AND Credit IS NOT NULL AND  (Debit > 0 OR Credit > 0)) Order by [Entry Number]";
  7.  
  8.             SqlDataAdapter da = new SqlDataAdapter(strSQL, conn);
  9.             DataSet ds = new DataSet();
  10.  
  11.             da.SelectCommand.Parameters.Add("@f", SqlDbType.DateTime).Value = ddtpFrom;
  12.             da.SelectCommand.Parameters.Add("@t", SqlDbType.DateTime).Value = ddtpTo;
  13.  
  14.             try
  15.             {
  16.                 conn.Open();
  17.  
  18.                 int i = ds.Tables["dsVoucher"].Rows.Count;
  19.  
  20.                 dataVoucher.AutoGenerateColumns = false;
  21.  
  22. dataVoucher.Columns.Add("GlcodeCol", "Gl Code")
  23.  
  24. //how do I bind Glcode from the SQL statement to GlcodeCol cells under the Gl Code header?
  25.  
  26.  
  27.                 dataVoucher.DataSource = ds;
  28.  
  29.                 dataVoucher.DataMember = "dsVoucher";
  30.  
  31.                 lbTotalRecords.Text = "Showing " + i + " records";
  32.             }
  33.             catch (Exception ex)
  34.             {
  35.                 lbTotalRecords.Text = ex.ToString();
  36.             }
  37.             finally
  38.             {
  39.                 conn.Close();
  40.             }
  41.  
  42.         }
Aug 17 '10 #1
1 2265
Christian Binder
218 Recognized Expert New Member
Try to set DataPropertyName-property of DataGridViewColumn, maybe this helps.
Expand|Select|Wrap|Line Numbers
  1. //line 22:
  2. int colIdx = dataVoucher.Columns.Add("GlcodeCol", "Gl Code");
  3. DataGridViewColumn column = dataVoucher.Columns[olcIdx];
  4. column.DataPropertyName = "代碼"; //set to the alias name of requested sql field
  5. // ...
  6.  
Aug 18 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Alpha | last post by:
I have several textboxes that I need to chang the text when the selection row is changed in a datagrid. I have the following code. This textbox displayes the initial selection but when I click on...
0
by: Anatoly | last post by:
For all projects we wrote base classes library. BObject is parent for all objects. It contains Fields Collection. Field is an object with Fields and Methods, for example Field has Value property...
2
by: Bennett Haselton | last post by:
I know how to create a DataAdapter that loads data from a data source into a table in a typed DataSet, and how to set the DataSource and DataMember properties of a DataGrid so that at run time it...
0
by: Paul | last post by:
Hi I have a datagrid with two different hyperlink columns. The columns open up a new window as the target is set to _blank for both. The windows are different sizes (one large, one small) and I...
5
by: Jay Villa | last post by:
Is it possible to update only few columns in datagrid during OnUpdateCommand event ? If so, could you help me .... -thanks Jay
1
by: andrewcw | last post by:
The only time the Mouse_Click event is firing for the DataGrid in my application is when I click on a column, What event should I be trapping ? Mouse Down ?? Thanks
2
by: Atley | last post by:
Any ideas on how to do this correctly so I can set the tablestyles and not have the table show up as a child? I keep getting stuck, so if anyone can help, it would be greatly appreciated!
2
by: Sean | last post by:
How does one bind a SortedList to anything. If it cant be done why would anyone ever even use a sortedlist? We are currently (via business objects that use interfaces etc) binding gridviews to...
1
by: vishwaskothari | last post by:
hiiiii how can i bind a datagrid control to a recordsource through programming. TIA regards vishwas
12
by: vishwaskothari | last post by:
How i can bind the datagrid with the record that are present in a recordsource through programming? one more thing is that the no of record present in the recordsource may vary according the users...
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
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,...
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
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,...
1
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.