473,320 Members | 1,982 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,320 software developers and data experts.

Last Row in Datagrid

Hi All,
I have a datagrid that reads from a data source with no problem. The problem is, after loading the data in the datagrid i have the last row empty by default ( the one with the asterics beside it ). this grid have aother child grid, so, when i navigate between the rows of the main grid the related data in the child grid changes according to the current selected row. So when ever i go to the empty row an error occurs. Simply i have to hide this empty row ( by the way, this row is used for new enteries ) and this can be done by changing the read only property of the main grid to True, the problem is i need to update the data in the main grid as well, and the read only mode prevents that.

Another question, is it possible to add a control in the grid itself. e.g. can i have a drop down menu shown inside the grid once i click inside a cell in a specific field. - I am using a data grid in WINDOWS FORMS application.

thanks a lot in advance.
Jul 4 '07 #1
4 5188
Dököll
2,364 Expert 2GB
Hi All,
I have a datagrid that reads from a data source with no problem. The problem is, after loading the data in the datagrid i have the last row empty by default ( the one with the asterics beside it ). this grid have aother child grid, so, when i navigate between the rows of the main grid the related data in the child grid changes according to the current selected row. So when ever i go to the empty row an error occurs. Simply i have to hide this empty row ( by the way, this row is used for new enteries ) and this can be done by changing the read only property of the main grid to True, the problem is i need to update the data in the main grid as well, and the read only mode prevents that.

Another question, is it possible to add a control in the grid itself. e.g. can i have a drop down menu shown inside the grid once i click inside a cell in a specific field. - I am using a data grid in WINDOWS FORMS application.

thanks a lot in advance.
Would you be able to post your code, AYasser?

I think you will get more hits this way, helps experts see what you're stuggling with straigh away and offer advice.

Let us know...

Welcome!
Jul 5 '07 #2
Killer42
8,435 Expert 8TB
It might be just the setting of .AllowAddNew which is causing the extra line. I believe the asterisk indicates it's a new entry, like you see at the bottom of the table/query grid when you work directly in MS Access

EDIT: Just had a look in the help. Here's what I found...

Remarks

If the AllowAddnew property is True, the last row displayed in the DataGrid control is left blank to permit users to enter new records. If the AllowAddNew property is False, no blank line is displayed.
.
Jul 5 '07 #3
Thanks a lot gents. i have had the problem solved yesterday by setting the data bindings of the datagrid to a dataview.
The AllowNew command is not accessible except through a dataview as i understood. but anyway it works fine right now thanks for your immediate reply.

The other question is still valid. Can i have a control embeded in a windows form datagrid.? e.g. can i have a dropdown list in a cell in a datagrid? so whenever i click a cell in a specific column in the grid it transforms to a dropdown list from which i can choose something , and my choice become the new cell value?
Jul 5 '07 #4
gtyler
8
"The other question is still valid. Can i have a control embeded in a windows form datagrid.? e.g. can i have a dropdown list in a cell in a datagrid? so whenever i click a cell in a specific column in the grid it transforms to a dropdown list from which i can choose something , and my choice become the new cell value?"


Yes you can!!

The DataGrid, just like a form has a Controls Collection, that can be added to.
In this bit of code I removed the text box controls for a specified column (as otherwise they appear over the top of the control I have added to the datagrid)

I have then added a button and comboBox with event handlers to a DataGrid called dgrUserDetails. These controls were declared at the top of the page like this:

Expand|Select|Wrap|Line Numbers
  1.     Dim ThisTexBox As New TextBox
  2.     Dim ThisButton As New Button
Expand|Select|Wrap|Line Numbers
  1.         'This stops the textbox from appearing over the top
  2.         dgrUserDetails.Controls.RemoveAt(10)
  3.         dgrUserDetails.Controls.RemoveAt(9)
  4.  
  5.         'This Creates the ComboBox
  6.         ThisComboBox.Items.Add("Read")
  7.         ThisComboBox.Items.Add("Full")
  8.         ThisComboBox.Items.Add("Admin")
  9.  
  10.         'And add it to the Datagrid's Controls collection
  11.         dgrUserDetails.Controls.Add(ThisComboBox)
  12.         ThisComboBox.Visible = False
  13.         'This gives it an event
  14.         AddHandler ThisComboBox.SelectedValueChanged, AddressOf OnComboBoxValueSelect
  15.  
  16.         dgrUserDetails.Controls.Add(ThisButton)
  17.         ThisButton.Visible = False
  18.         ThisButton.Text = "Change"
  19.  
  20.         ThisButton.Font = New Font(ThisButton.Font.FontFamily, 7, FontStyle.Regular, GraphicsUnit.Point)
  21.         AddHandler ThisButton.Click, AddressOf OnButtonClick

The other thing I think you would want to do is control where the control appears. This bit of code make the control appear over the cell you have clicked:

Expand|Select|Wrap|Line Numbers
  1.     Private Sub dgrUserDetails_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgrUserDetails.CurrentCellChanged
  2.         'This prevents them from being visible if focus changes
  3.         ThisComboBox.Visible = False
  4.         ThisButton.Visible = False
  5.         Dim ThisControl As Control
  6.  
  7.         'This works out the Column selected
  8.         If dgrUserDetails.CurrentCell.ColumnNumber = 9 Then
  9.             'This makes it visble
  10.             ThisComboBox.Visible = True
  11.  
  12.             'It sets it's position to be the same as the Current Cell
  13.             ThisComboBox.Location = New Point(dgrUserDetails.GetCurrentCellBounds.X, dgrUserDetails.GetCurrentCellBounds.Y)
  14.             ThisComboBox.Height = dgrUserDetails.GetCurrentCellBounds.Height
  15.             ThisComboBox.Width = dgrUserDetails.GetCurrentCellBounds.Width
  16.         ElseIf dgrUserDetails.CurrentCell.ColumnNumber = 8 Then
  17.             ThisButton.Visible = True
  18.             ThisButton.Location = New Point(dgrUserDetails.GetCurrentCellBounds.X, dgrUserDetails.GetCurrentCellBounds.Y)
  19.             ThisButton.Height = dgrUserDetails.GetCurrentCellBounds.Height
  20.             ThisButton.Width = dgrUserDetails.GetCurrentCellBounds.Width
  21.         End If
  22.     End Sub
Finally you would want it to update the right cell!! This one is for the ComboBox

Expand|Select|Wrap|Line Numbers
  1.     Private Sub OnComboBoxValueSelect(ByVal sender As Object, ByVal e As System.EventArgs)
  2.         Dim ThisComboBox As ComboBox = CType(sender, ComboBox)
  3.         'This now makes the cell's value match that of the combo box
  4.         dgrUserDetails.Item(dgrUserDetails.CurrentCell.RowNumber, dgrUserDetails.CurrentCell.ColumnNumber) = ThisComboBox.SelectedItem
  5.         ThisComboBox.Visible = False
  6.     End Sub
I hope this makes some sort of sense.

Best Wishes G.
Jul 5 '07 #5

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

Similar topics

0
by: vincent | last post by:
Hello, How do i get the last row OnItemDataBound in Datagrid or atleast the number of rows OnItemDataBound in Datagrid? thanks Vincent
1
by: Ryan Liu | last post by:
Hi, when I edit a cell in datagrid, I need click on another cell to save the current cell. But when I close the form, the last modified cell is not changed, how can I prevent this? Thanks a...
3
by: Ryan Liu | last post by:
Hi there, I got a NullReferenceException when delete last row in a datagrid. I had hard time to solve since it does not occur in my own code. I put a datagrid in my inherited user control,...
4
by: Peter Afonin | last post by:
Hello: When I print the report based on the datagrid content, often the last line on the page is cut in half, and the next half appears on the next page. How can I format the page to avoid this?...
1
by: Hajime Kusakabe | last post by:
I made a datagrid and it contains several columns. The number of columns depend on what users select from a dropdown list. Therefore the number of columns changes. I would like to find the last...
0
by: Diego F. | last post by:
I'm trying to print a DataGrid. I use javascript and can print it but the last line gets cut, as well as I don't have the control horizontally. How do you print a DataGrid? -- Regards, ...
2
by: tupolev | last post by:
Hello Is there a way to find the last row in the datagrid BEFORE I save the changes to the table? Tupolev
1
by: beachboy | last post by:
any most fast method that we can know the idicate the first row and last row in datagrid? ..NET 1.1 Thanks in advanced.
3
by: =?Utf-8?B?UGhpbCBKb2huc29u?= | last post by:
I have data that spans over several pages in a datagrid. On the last page, the row heights seem to justify vertically, so if there are say 14 records and the paging is ten, each row on the last...
0
by: marlberg | last post by:
Platform: Windows2000, WindowsXP, Windows Vista, etc Language: C#, ASP.NET Pre-compiled Libraries: Enterprise Library 3.0 full I have a requirement to implement in and display in C# and...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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...
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)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.