473,386 Members | 2,078 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,386 software developers and data experts.

gridview editing

hi i am not getting the editable gridview using code behind...... i am using mysql and vb for coding please help me.......



asp:DataGrid ID="dg1" Style="z-index: 101; left: 160px; position: absolute; top: 112px"
runat="server" AutoGenerateColumns="False" DataKeyField="statesId" OnEditCommand="DoItemEdit"
OnDeleteCommand="DoItemDelete" OnUpdateCommand="DoItemUpdate" BackColor="White"
BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black"
GridLines="Vertical">
<Columns>
<asp:BoundColumn Visible="False" DataField="stateName" HeaderText="stateName"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="stateName">
<ItemTemplate>
<asp:Label ID="Label3" Text='<%# Container.DataItem("stateName") %>' runat="server">
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtrolesDes" runat="server"></asp:TextBox>
</FooterTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtrolesDes" runat="server" Text='<%# container.dataitem("stateName") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Modify">
<ItemTemplate>
<asp:LinkButton ID="Linkbutton2" runat="server" CausesValidation="false" CommandName="Edit"
Text="Edit"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:Button CommandName="Insert" Text="Add" ID="btnAdd" runat="server" />
</FooterTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton3" runat="server" CommandName="Update" Text="Update"></asp:LinkButton>&nbsp;&nbsp;&nbsp;&nbsp;
<asp:LinkButton ID="Linkbutton4" runat="server" CausesValidation="false" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<SelectedItemStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<AlternatingItemStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
</asp:DataGrid>
</div>




Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strcon As String
strcon = "server=localhost; user id=root; password=showoff; database=perry24"
Dim conn2 As New MySqlConnection(strcon)
Dim strSQL2 As String
strSQL2 = "SELECT * FROM limostates"
Try
conn2.Open()
Dim dataadapter As New MySqlDataAdapter(strSQL2, strcon)
Dim dataset1 As New DataSet
dataadapter.Fill(dataset1)
dg1.DataSource = dataset1
dg1.DataBind()
Catch ex As MySql.Data.MySqlClient.MySqlException
Response.Write(ex.Message)
End Try
conn2.Close()
End Sub
Sub DoItemDelete(ByVal objSource As Object, ByVal objArgs As DataGridCommandEventArgs)
Dim strcon As String
strcon = "server=localhost; user id=root; password=showoff; database=perry24"
Dim conn2 As New MySqlConnection(strcon)
Dim strSQL2 As String
Try
conn2.Open()
strSQL2 = "DELETE FROM limostates WHERE statesId=?statesId"
Dim cmd As New MySqlCommand(strSQL2, conn2)
cmd.ExecuteNonQuery()
strSQL2 = "SELECT * FROM limostates"
Dim dataadapter As New MySqlDataAdapter(strSQL2, strcon)
Dim dataset1 As New DataSet
dataadapter.Fill(dataset1)
dg1.DataSource = dataset1
dg1.DataBind()
Catch ex As MySql.Data.MySqlClient.MySqlException
Response.Write(ex.Message)
End Try
conn2.Close()
End Sub

Sub DoItemEdit(ByVal objSource As Object, ByVal objArgs As DataGridCommandEventArgs)
dg1.EditItemIndex = objArgs.Item.ItemIndex
End Sub

Sub DoItemCancel(ByVal objSource As Object, ByVal objArgs As DataGridCommandEventArgs)
dg1.EditItemIndex = -1
End Sub

Sub DoItemUpdate(ByVal objSource As Object, ByVal objArgs As DataGridCommandEventArgs)
Dim rolesDes As TextBox
Dim statesId As Integer
Dim qstring As String
rolesDes = CType(objArgs.Item.FindControl("rolesDes"), TextBox)
statesId = dg1.DataKeys(objArgs.Item.ItemIndex)
Dim strcon As String
strcon = "server=localhost; user id=root; password=showoff; database=perry24"
Dim conn2 As New MySqlConnection(strcon)
Try
conn2.Open()
qstring = "UPDATE limostates set stateName='" & rolesDes.Text & "' WHERE statesId=?statesId"
Dim cmd As New MySqlCommand(qstring, conn2)
cmd.ExecuteNonQuery()
dg1.EditItemIndex = -1
qstring = "SELECT * FROM limostates"
Dim dataadapter As New MySqlDataAdapter(qstring, conn2)
Dim dataset1 As New DataSet
dataadapter.Fill(dataset1)
dg1.DataSource = dataset1
dg1.DataBind()
Catch ex As MySql.Data.MySqlClient.MySqlException
Response.Write(ex.Message)
End Try
conn2.Close()
End Sub
End Class
Mar 4 '08 #1
5 1316
Hi,

I have gone through your code. But i did not get one thing that why did you make the states column as visible false.

<asp:BoundColumn Visible="False" DataField="stateName" HeaderText="stateName"></asp:BoundColumn>


If you are facing any perticular exception then you can post that here.

Thanks
Mahesh



hi i am not getting the editable gridview using code behind...... i am using mysql and vb for coding please help me.......



asp:DataGrid ID="dg1" Style="z-index: 101; left: 160px; position: absolute; top: 112px"
runat="server" AutoGenerateColumns="False" DataKeyField="statesId" OnEditCommand="DoItemEdit"
OnDeleteCommand="DoItemDelete" OnUpdateCommand="DoItemUpdate" BackColor="White"
BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black"
GridLines="Vertical">
<Columns>
<asp:BoundColumn Visible="False" DataField="stateName" HeaderText="stateName"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="stateName">
<ItemTemplate>
<asp:Label ID="Label3" Text='<%# Container.DataItem("stateName") %>' runat="server">
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtrolesDes" runat="server"></asp:TextBox>
</FooterTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtrolesDes" runat="server" Text='<%# container.dataitem("stateName") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Modify">
<ItemTemplate>
<asp:LinkButton ID="Linkbutton2" runat="server" CausesValidation="false" CommandName="Edit"
Text="Edit"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:Button CommandName="Insert" Text="Add" ID="btnAdd" runat="server" />
</FooterTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton3" runat="server" CommandName="Update" Text="Update"></asp:LinkButton>&nbsp;&nbsp;&nbsp;&nbsp;
<asp:LinkButton ID="Linkbutton4" runat="server" CausesValidation="false" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<SelectedItemStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<AlternatingItemStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
</asp:DataGrid>
</div>




Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strcon As String
strcon = "server=localhost; user id=root; password=showoff; database=perry24"
Dim conn2 As New MySqlConnection(strcon)
Dim strSQL2 As String
strSQL2 = "SELECT * FROM limostates"
Try
conn2.Open()
Dim dataadapter As New MySqlDataAdapter(strSQL2, strcon)
Dim dataset1 As New DataSet
dataadapter.Fill(dataset1)
dg1.DataSource = dataset1
dg1.DataBind()
Catch ex As MySql.Data.MySqlClient.MySqlException
Response.Write(ex.Message)
End Try
conn2.Close()
End Sub
Sub DoItemDelete(ByVal objSource As Object, ByVal objArgs As DataGridCommandEventArgs)
Dim strcon As String
strcon = "server=localhost; user id=root; password=showoff; database=perry24"
Dim conn2 As New MySqlConnection(strcon)
Dim strSQL2 As String
Try
conn2.Open()
strSQL2 = "DELETE FROM limostates WHERE statesId=?statesId"
Dim cmd As New MySqlCommand(strSQL2, conn2)
cmd.ExecuteNonQuery()
strSQL2 = "SELECT * FROM limostates"
Dim dataadapter As New MySqlDataAdapter(strSQL2, strcon)
Dim dataset1 As New DataSet
dataadapter.Fill(dataset1)
dg1.DataSource = dataset1
dg1.DataBind()
Catch ex As MySql.Data.MySqlClient.MySqlException
Response.Write(ex.Message)
End Try
conn2.Close()
End Sub

Sub DoItemEdit(ByVal objSource As Object, ByVal objArgs As DataGridCommandEventArgs)
dg1.EditItemIndex = objArgs.Item.ItemIndex
End Sub

Sub DoItemCancel(ByVal objSource As Object, ByVal objArgs As DataGridCommandEventArgs)
dg1.EditItemIndex = -1
End Sub

Sub DoItemUpdate(ByVal objSource As Object, ByVal objArgs As DataGridCommandEventArgs)
Dim rolesDes As TextBox
Dim statesId As Integer
Dim qstring As String
rolesDes = CType(objArgs.Item.FindControl("rolesDes"), TextBox)
statesId = dg1.DataKeys(objArgs.Item.ItemIndex)
Dim strcon As String
strcon = "server=localhost; user id=root; password=showoff; database=perry24"
Dim conn2 As New MySqlConnection(strcon)
Try
conn2.Open()
qstring = "UPDATE limostates set stateName='" & rolesDes.Text & "' WHERE statesId=?statesId"
Dim cmd As New MySqlCommand(qstring, conn2)
cmd.ExecuteNonQuery()
dg1.EditItemIndex = -1
qstring = "SELECT * FROM limostates"
Dim dataadapter As New MySqlDataAdapter(qstring, conn2)
Dim dataset1 As New DataSet
dataadapter.Fill(dataset1)
dg1.DataSource = dataset1
dg1.DataBind()
Catch ex As MySql.Data.MySqlClient.MySqlException
Response.Write(ex.Message)
End Try
conn2.Close()
End Sub
End Class
Mar 5 '08 #2
thanks mahesh. i solved that problem..... is it possible to display the gridview vertically in the source code....
Mar 7 '08 #3
Hi,

What do you mean by Vertical....do you mean in landscape format?

Thanks
Mahesh



thanks mahesh. i solved that problem..... is it possible to display the gridview vertically in the source code....
Mar 10 '08 #4
colulmns datas should be displayed in row.....
Mar 10 '08 #5
Hi,

That is possible. What you have to do is

1. Take your dataset in one datatable.

2. for intj=0;j<datatable.columns.count-1;j++
datarow dr = new datarow()
datatable2.rows.add(dr)
for i=0;i<datatable.rows.count-1;i++
dr[i] =datatable2[i][j]

Like wise you have to transfer data from column to rows of new datatatble.

And then bind that datatble to datagrid.

Thanks
Mahesh
Mar 11 '08 #6

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

Similar topics

1
by: j.zascinski | last post by:
Hi, i have a "simple" problem with gridview, please help me :) i want to have gridview which is binded to a datatable (or a dataset). i can show the data from the dataset in the gridview and i can...
4
by: P. Yanzick | last post by:
Hello, I've been playing with master/detail views as well as editing in the gridview, and I ran across a strange problem that I am not exactly sure where to go to try to solve. I have 2...
1
by: Kyle K. | last post by:
I would like to build my data pages such that the top of the page contains a GridView showing the data with 'Enable Select = true'. Below that I would like to have a FormView, that by default is...
4
by: Tomasz Jastrzebski | last post by:
Hello Everyone, I have a GridView control bound to a plain DataTable object. AutoGenerateEditButton is set to true, Edit button gets displayed, and RowEditing event fires as expected.
3
by: cpnet | last post by:
I have a GridView which I'm populating from an ObjectDataSource (give the GridView a DataTable). The GridView will have about 20 rows, and only one editable column. The editable column consists...
1
by: =?Utf-8?B?Q2hyaXM=?= | last post by:
Hi, I have a gridview which I added a <asp:CommandField EditText="E" CancelText="C" UpdateText="U" ButtonType="Link" ShowEditButton="True" /> my gridview looks like this <asp:GridView...
1
by: pieandpeas | last post by:
he selectedindex of my gridview is always one behind for example, if i click on the EDIT.gif on row 1, then 5 , then 7, if i break the code and check the selectedindex, it is equal to 5. if i...
5
by: =?Utf-8?B?Z3V5?= | last post by:
How do you enable editing in a GridView programatically rather than via its Tasks menu? Guy
2
by: Michael | last post by:
It seems that a gridview allows us to delete only a single row at a time. How to extend this functionality to select multiple rows and delete all of the selected rows in a single stroke? just like...
11
by: SAL | last post by:
Hello, I have a Gridview control (.net 2.0) that I'm having trouble getting the Update button to fire any kind of event or preforming the update. The datatable is based on a join so I don't know...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.