473,508 Members | 2,489 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Grid View - disconnected datasource

15 New Member
i used Grid view and here is the code i wrote:
Expand|Select|Wrap|Line Numbers
  1. Protected Sub ButtonAddFile_Click(ByVal sender As Object, ByVal e As EventArgs)
  2. Dim gv As New GridView
  3.         gv.Style.Add("position", "absolute")
  4.         gv.Style.Add("left", "575px")
  5.         gv.Style.Add("top", "220px")
  6.         gv.EnableViewState = False
  7.         form1.Controls.Add(gv)
  8.         gv.DataSource = GetDataTable()
  9.         gv.DataBind()
  10.         Dim lbl As New Label
  11.         lbl.Style.Add("position", "absolute")
  12.         lbl.Style.Add("left", "575px")
  13.         lbl.Style.Add("top", "20px")
  14. End Sub
  15. Private Function GetDataTable() As DataTable 
  16.     Dim doc As New DataTable("Document")
  17.     Dim id As New DataColumn("Document") 
  18.  
  19.     id.DataType = GetType(String)
  20.     id.Unique = True
  21.     id.AllowDBNull = False
  22.     id.Caption = "Document"
  23.  
  24.     doc.Columns.Add(id)
  25.  
  26.     Dim filing As New DataColumn("Type") 
  27.     filing.DataType = GetType(String)
  28.     'filing.MaxLength = 35
  29.     filing.AllowDBNull = False
  30.  
  31.     doc.Columns.Add(filing)
  32.     doc.PrimaryKey = New DataColumn() {id} 
  33.     'add data to data table
  34.  
  35.     Dim newdoc As DataRow = doc.NewRow() 
  36.     newdoc("Document") = FileToUpload.PostedFile.FileName
  37.     newdoc("Type") = listDocType.SelectedValue
  38.  
  39.     doc.Rows.Add(newdoc)
  40.     Return doc 
  41. End Function
  42.  
1. But it adds only one row everytime i click the AddFile button and overrides the previous row, it doesnt add on after another, whats going wrong here?
2. Also If I have to add a delete button of GridView , what code should i write so that it deletes that particular row every time i click delete
Sep 6 '07 #1
5 1691
Frinavale
9,735 Recognized Expert Moderator Expert
Hi Sspost,

I have moved your question to the .NET Forums section. The .NET Articles section is reserved for "how-tos" and other informational articles and is not meant for your questions. You will receive help with your problems in the .NET Forums section so please post any further questions here (Blue menu: Forums->.NET)

Thanks,

-Moderator
Sep 6 '07 #2
Plater
7,872 Recognized Expert Expert
You need to be adding your rows to your dataobject, everytime you change the datasource property it wipes out all your old values and re-creates from the new ones.
Maintain a DataTable object and add rows to that.
Sep 6 '07 #3
sspost
15 New Member
[Ok , I used dataobject and can add rows one after another, but i added delete button but still cant get the delete to delete the rows from gridview ...here is the code i wrote:
Expand|Select|Wrap|Line Numbers
  1. <asp:datagrid id="DataGrid1" runat="server" Width="100%" AutoGenerateColumns="False">
  2.                  <AlternatingItemStyle BackColor="beige"></AlternatingItemStyle>
  3.                  <ItemStyle Font-Size="Smaller" HorizontalAlign="Center" BackColor="LightGray"></ItemStyle>
  4.                  <HeaderStyle Font-Size="Smaller" Font-Bold="True" HorizontalAlign="Center" ForeColor="White"
  5.                      BackColor="DimGray"></HeaderStyle>
  6.                    <Columns>
  7.                       <asp:BoundColumn DataField="Document" HeaderText="Document"></asp:BoundColumn>
  8.                      <asp:BoundColumn DataField="Document Title" HeaderText="Document Title"></asp:BoundColumn>
  9.                      <asp:ButtonColumn commandname="Delete" headertext="Delete" Text="Delete"></asp:ButtonColumn>
  10.                    </Columns>
  11.            </asp:datagrid>
  12.  
here is the code:
Expand|Select|Wrap|Line Numbers
  1. Protected Sub ButtonDeleteFile_Click(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Handles DataGrid1.ItemCommand
  2.         If Not e.CommandName <> "Delete" Then
  3.             Return
  4.  
  5.             Dim sessionObject As Object = Session("docDataTable")
  6.  
  7.             If Not (sessionObject Is Nothing) Then
  8.                 Dim idValue As String = e.Item.Cells(0).Text
  9.  
  10.                 Dim doc As DataTable = CType(sessionObject, DataTable)
  11.                 Dim sqlFilter As String = "Document = '" + idValue + "'"
  12.                 Dim filteredArray As DataRow() = doc.Select(sqlFilter)
  13.                 If filteredArray.Length > 0 Then
  14.                     Dim filteredRow As DataRow = filteredArray(0)
  15.                     doc.Rows.Remove(filteredRow)
  16.                 End If
  17.             End If
  18.         End If
  19.  
  20.     End Sub
  21.  
But this doesnt even get to the if stmt and i dont know why VisualStudio is says "unused local variable" for each and every varaible used here.
Sep 6 '07 #4
Plater
7,872 Recognized Expert Expert
Do you have more then one function handling that event?
Sep 6 '07 #5
sspost
15 New Member
No I dont
Now I am getting this :
at the following line:

doc.Rows.Remove(filteredRow)

it gives me exception: System.IndexOutOfRangeException - The given DataRow is not in the current DataRowCollection. , even though the search row is the primary key one........does anyone has any idea why its giving it? even though the comparing row is the correct one.....
Sep 6 '07 #6

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

Similar topics

5
2475
by: pmud | last post by:
Hi, I need to display columns in a data grid based on 7 different queries. Now I have 32 questions: 1. Is it possble to have 1 single data adapter with 7 queries & 1 data set or do I need to...
12
2365
by: pmud | last post by:
Hi, I am using teh following code for sorting the data grid but it doesnt work. I have set the auto generate columns to false. & set the sort expression for each field as the anme of that...
4
2833
by: Filippo Pandiani | last post by:
I have a grid that shows the file list from a folder. On the postback, how do I get a Dataset from this grid? Thanks, Filippo.
5
259
by: pmud | last post by:
Hi, I need to display columns in a data grid based on 7 different queries. Now I have 32 questions: 1. Is it possble to have 1 single data adapter with 7 queries & 1 data set or do I need to...
0
7133
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
7336
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
7066
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
7504
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...
0
5643
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,...
1
5059
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
4724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3214
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...
0
1568
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.