Edit in Datagrid
Edit in Datagrid
Edit in Datagrid
Edit in Datagrid
Edit in Datagrid
Edit in Datagrid Edit in Datagrid Edit in Datagrid Edit in Datagrid Edit in Datagrid Edit in Datagrid Edit in Datagrid Edit in Datagrid
Edit in Datagrid Edit in Datagrid
Edit in Datagrid
Go Back  Xtreme Visual Basic Talk > > > Edit in Datagrid


Reply
 
Thread Tools Display Modes
  #1  
Old 03-14-2007, 11:29 AM
goharulislam goharulislam is offline
Regular
 
Join Date: Jun 2003
Posts: 74
Default Edit in Datagrid


Following code show textboxes in row of a grid through this. But it is not working. Actually I am not understanding problem in following code and why it is giving problem in word item in e.item.itemindex

Sub EditGridData(Sender As Object,E As EventArgs)

dg.EditItemIndex = CInt(E.Item.ItemIndex)
ShowData()

End Sub

Last edited by goharulislam; 03-15-2007 at 05:34 AM.
Reply With Quote
  #2  
Old 03-21-2007, 04:42 PM
MKoslof's Avatar
MKoslofEdit in Datagrid MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

We need more information to help. A standard EventArg object doesn't contain an item object or ItemIndex, it appears you are confusing a DataGridViewArgs object or a similiar event argument derived type.

Also, if you can show us some of the code in ShowData() that would probably help too
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #3  
Old 03-24-2007, 05:31 AM
goharulislam goharulislam is offline
Regular
 
Join Date: Jun 2003
Posts: 74
Default

Sub ShowData()

Dim strCN as String="Provider=Microsoft.Jet.Oledb.4.0;Data Source=E:\Gohar\ASP\Accounts.mdb"
Dim strselect as String="SELECT * FROM ACCOUNTHOLDERS"

Dim objDataSet as new DataSet()
Dim objCN as new OledbConnection(strCN)
Dim objAdapter as new OledbDataAdapter(strselect,objCN)
objAdapter.fill(objDataSet,"AccountHolders")
dg.DataSource = objDataSet.tables("AccountHolders").Defaultview
dg.DataBind()
objCN.close()

end sub

Thankyou for ur interest
Reply With Quote
  #4  
Old 03-29-2007, 01:46 AM
goharulislam goharulislam is offline
Regular
 
Join Date: Jun 2003
Posts: 74
Default

Hey I got the Mistake here it is c the difference, I was not writting the complete sentence which is "DataGridCommandEventArgs"

Sub EditGridData(Sender As Object,e As DataGridCommandEventArgs)
dg.EditItemIndex=cint(E.Item.ItemIndex)
ShowData()
End Sub
Reply With Quote
  #5  
Old 03-29-2007, 07:53 PM
MKoslof's Avatar
MKoslofEdit in Datagrid MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

What exactly are you trying to accomplish here? That is more of my question

Code:
dg.EditItemIndex=cint(E.Item.ItemIndex)
If you are looking to do something within the cell or item properties while the grid is binding or formatting there are specific grid events which can be used such as Row Item Bound, Cell Formatting, etc.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #6  
Old 03-30-2007, 11:16 AM
goharulislam goharulislam is offline
Regular
 
Join Date: Jun 2003
Posts: 74
Default

ya I want to locate row in grid for editing
any good tutorial on "editing in datagrid" bcoz I m stucked again in

<ItemTemplate>
<asp:label runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "title") %>'/>
</ItemTemplate>

and on above code the error comming is "Compiler Error Message: BC30456: 'DataItem' is not a member of 'System.Web.UI.Page'."
Reply With Quote
  #7  
Old 03-31-2007, 06:41 PM
MKoslof's Avatar
MKoslofEdit in Datagrid MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

So is this a web or windows app? Looks like from your last piece of code this an ASP.net application. Can you show us more of your ASP.net mark up code?
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #8  
Old 04-02-2007, 09:52 AM
goharulislam goharulislam is offline
Regular
 
Join Date: Jun 2003
Posts: 74
Default

Sir, I have worked a bit more over it and now it is saying "'DataItem' is not a member of 'System.Web.UI.Page'". I am pasting the whole code in the next message, I am thankfull of you that you are taking so much interest.
Take care
goharulislam@gmail.com
Reply With Quote
  #9  
Old 04-02-2007, 09:53 AM
goharulislam goharulislam is offline
Regular
 
Join Date: Jun 2003
Posts: 74
Default

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Oledb" %>

<script runat="server" language="VB" >



'backend: D:\Gohar\Accounts.mdb - MS Access
'To view and edit Data
'id of DataGrid is dg



Sub Page_Load(Sender As Object, E As System.Web.UI.Webcontrols.DataGridCommandEventArgs)

if not Page.IsPostBack then
ShowData()
end if

end sub
'---------------------------------------------------------------------------------------------------------------------------------------------
Sub ShowData()

Dim strCN as String="Provider=Microsoft.Jet.Oledb.4.0;Data Source=E:\Gohar\ASP\Accounts.mdb"
Dim strselect as String="SELECT * FROM ACCOUNTHOLDERS"

Dim objDataSet as new DataSet()
Dim objCN as new OledbConnection(strCN)
Dim objAdapter as new OledbDataAdapter(strselect,objCN)

objAdapter.fill(objDataSet,"AccountHolders")
dg.DataSource = objDataSet.tables("AccountHolders").Defaultview
dg.DataBind()
objCN.close()

end sub
'---------------------------------------------------------------------------------------------------------------------------------------------
Sub EditGridData(Sender As Object,E As System.Web.UI.Webcontrols.DataGridCommandEventArgs)

dg.EditItemIndex=cint(E.Item.ItemIndex)
ShowData()

End Sub
'---------------------------------------------------------------------------------------------------------------------------------------------
Sub CancelGridData(Sender As Object,E As System.Web.UI.Webcontrols.DataGridCommandEventArgs)

dg.EditItemIndex=-1
ShowData()

End Sub
'---------------------------------------------------------------------------------------------------------------------------------------------
Sub UpdateGridData(Sender As Object,E As System.Web.UI.WebControls.DataGridCommandEventArgs)

Dim objCN2 as OledbConnection
Dim objCommand as OledbCommand
Dim strUpdate as String
Dim objTextBox as TextBox
Dim strCN2 as String="Provider=Microsoft.Jet.Oledb.4.0;Data Source=D:\Gohar\Accounts.mdb"

objCN2 = new OledbConnection(strCN2)
objCN2.Open()

strUpdate="UPDATE ACCOUNTHOLDERS SET TITLE= @TITLE, TYPE= @TYPE WHERE AccNo=@AccNo"
'updating data in data source
objCommand=New OledbCommand(strUpdate,objCN2)

objCommand.Parameters.Add(New oledbParameter("@AccNo", oledbType.varchar,9))
objCommand.Parameters.Add(New oledbParameter("@Title", oledbType.varchar,50))
objCommand.Parameters.Add(New oledbParameter("@Type", oledbType.varchar,50))

objTextBox=E.Item.findControl("edit_title")
objCommand.Parameters("@TITLE").value = objTextBox.text
objTextBox=E.Item.findControl("edit_type")
objCommand.Parameters("@TYPE").value = objTextBox.text

objCommand.ExecuteNonQuery()
dg.EditItemIndex=-1
objCN2.Close()

lblStatus.Text="Succesfully Updated in Database"

ShowData()

end sub
'---------------------------------------------------------------------------------------------------------------------------------------------
</script>

<html><title>Edit in Datagrid</title>

<form runat="server" language="VB">

<asp:datagrid id="dg" width="100%" runat="server"/>
<HeaderStyle ForeColor="White" BackColor="Red" Font-Bold="True"/>
<ItemStyle BackColor="White"/>
<AlternatingItemStyle BackColor="GainsBoro"/>
<OnEditCommand="EditGridData"/>
<OnCancelCommand"CancelGridData"/>
<OnUpdateCommand"UpdateGridData"/>
</asp:datagrid></p>

<columns>
<asp:EditCommandColumn
EditText="Edit"
CancelText="Cancel"
UpdateText="Update"
ItemStyle-Wrap="False"/>

<asp:BoundColumn
DataField="AccNo"
Visible="True"/>

<asp:TemplateColumn
HeaderText="Title"
ItemStyle-Wrap="False">

<ItemTemplate>
<asp:label runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "title") %>'/>
</ItemTemplate>

<EditItemTemplate>
<asp:textbox runat="server"
id="edit_title"
Font-name="verdana"
Font-size="8pt"
width="425"
Text='<%# DataBinder.Eval(Container.DataItem, "title") %>'/>
</EditItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn
HeaderText="Type"
ItemStyle-Wrap="False">

<ItemTemplate>
<asp:label runat="server"
text='<%# DataBinder.Eval (Container.DataItem, "type") %>'/>
</ItemTemplate>

<EditItemTemplate>
<asp:textbox runat="server"
id="edit_type"
Font-name="verdana"
Font-size="8pt"
width="25"
Text='<%# DataBinder.Eval(Container.DataItem, "type") %>'/>
</EditItemTemplate>
</asp:TemplateColumn>

</columns>

</asp:datagrid>

<asp:label id="lblStatus" runat="server"/><br/>

</form>
<body></body>
</html>
Reply With Quote
  #10  
Old 04-02-2007, 02:07 PM
MKoslof's Avatar
MKoslofEdit in Datagrid MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

1) Are you DataBinding the GridView?
2) Are you setting AutoGenerateColumns to false?
3) An example of one of your TemplateColumns

Code:
<ItemTemplate>
<asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.MyField") %>'>
</asp:Label>
</ItemTemplate>
I believe this is your problem area, but I am having a hard time following the code and where the actual problem is. Also in this routine which is set up as GridView event handler

Code:
Sub EditGridData(Sender As Object,E As System.Web.UI.Webcontrols.DataGridCommandEventArgs)

dg.EditItemIndex=cint(E.Item.ItemIndex)
ShowData()

End Sub
What is it that you are trying to do? It appears you are trying to set some item index, but then you are calling ShowData() again which will reload the grid. And you also are contending with postbacks here.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown

Last edited by MKoslof; 04-02-2007 at 02:19 PM.
Reply With Quote
  #11  
Old 04-04-2007, 07:51 AM
wayneph's Avatar
waynephEdit in Datagrid wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

In 2.0, you don't have to code this by hand.

http://msdn.microsoft.com/msdnmag/is...efault.aspx#S6
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
  #12  
Old 04-05-2007, 10:58 AM
goharulislam goharulislam is offline
Regular
 
Join Date: Jun 2003
Posts: 74
Default

I must ask now any new grid then gridview available? because of ease gridview has. Well, thankyou Mr. Koslof and Mr. Wayneph for spending your valuable time on my poor code. I am really greatfull of both you. I am starting work over gridview and I am hopefull that you guys will help me in future as well.
Thankyou very much
Take care
Reply With Quote
  #13  
Old 04-05-2007, 05:17 PM
MKoslof's Avatar
MKoslofEdit in Datagrid MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

The DataGridView object is the newest grid control in the 2.0 framework.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #14  
Old 04-07-2007, 04:31 AM
goharulislam goharulislam is offline
Regular
 
Join Date: Jun 2003
Posts: 74
Default

Thankyou Mr. Kolsof.
Following are great & very easy tutorials for GridView specially

http://msconline.maconstate.edu/tuto...20/default.htm
http://msconline.maconstate.edu/tuto...pnet09-01.aspx
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Edit in Datagrid
Edit in Datagrid
Edit in Datagrid Edit in Datagrid
Edit in Datagrid
Edit in Datagrid
Edit in Datagrid Edit in Datagrid Edit in Datagrid Edit in Datagrid Edit in Datagrid Edit in Datagrid Edit in Datagrid
Edit in Datagrid
Edit in Datagrid
 
Edit in Datagrid
Edit in Datagrid
 
-->