bang head here
Member
I have some code that makes a DataGrid editable. When you
click on "Edit" the values are shown in a text box and then you
have the ability to edit the text and update the record.
Fantastic. Heres where things go horrible wrong. What Im
looking to do is to not have those text boxes, but rather
dropdownlists populated from a couple queries. So that like the
field "Name" is taken from a query.
Well I cant seem to find where the code is placed to create the
text boxes. In fact there isnt any. So my question is...is there
some attribute that Im just not seeing that creates these text
boxes? And can I do my dropdownlist idea in a DataGrid? This is
exactly what Ive been looking for, and the idea is great. Im
afraid thatIm trying to do too much in the DataGrid.
Below is the file. Maybe someone can see something that I just cant.
Many thanks!
[edit]formated page width[/edit]
click on "Edit" the values are shown in a text box and then you
have the ability to edit the text and update the record.
Fantastic. Heres where things go horrible wrong. What Im
looking to do is to not have those text boxes, but rather
dropdownlists populated from a couple queries. So that like the
field "Name" is taken from a query.
Well I cant seem to find where the code is placed to create the
text boxes. In fact there isnt any. So my question is...is there
some attribute that Im just not seeing that creates these text
boxes? And can I do my dropdownlist idea in a DataGrid? This is
exactly what Ive been looking for, and the idea is great. Im
afraid thatIm trying to do too much in the DataGrid.
Below is the file. Maybe someone can see something that I just cant.
Many thanks!
Code:
<%@ Page Language="VB" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">
Public Sub Page_Load(Source As Object, E As EventArgs)
If Not Page.IsPostBack Then
BindData()
End If
End Sub
Public Sub DataGrid_Edit(Source As Object, E As DataGridCommandEventArgs)
DailyGrid.EditItemIndex = E.Item.ItemIndex
BindData()
End Sub
Public Sub DataGrid_Cancel(Source As Object, E As DataGridCommandEventArgs)
DailyGrid.EditItemIndex = -1
BindData()
End Sub
Public Sub DataGrid_Update(Source As Object, E As DataGridCommandEventArgs)
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
record update stuff
Dim txtName As TextBox = E.Item.Cells(2).Controls(0)
Dim txtAddress As TextBox = E.Item.Cells(3).Controls(0)
Dim strUpdateStmt As String
strUpdateStmt = "UPDATE Person SET " & _
"Name = " & txtName.Text & ", " & _
"Address = " & txtAddress.Text & " " & _
"WHERE ID = " & E.Item.Cells(1).Text
myConnection = New SqlConnection( _
"server=eclipse;database=CitadelSQL;uid=sa;pwd=;")
myCommand = New SqlCommand(strUpdateStmt, myConnection)
myConnection.Open()
myCommand.ExecuteNonQuery()
DailyGrid.EditItemIndex = -1
BindData()
End Sub
Public Sub BindData()
Dim myDataSet As New DataSet
Dim mySqlDataAdapter As SqlDataAdapter
mySqlDataAdapter = New SqlDataAdapter( _
"SELECT * FROM Person", _
"server=eclipse;database=CitadelSQL;uid=sa;pwd=;")
mySqlDataAdapter.Fill(myDataSet, "Person")
DailyGrid.DataSource = myDataSet.Tables("Person")
DailyGrid.DataBind()
End Sub
</script>
<html>
<head>
</head>
<body>
<form method="post" runat="server">
<p>
<asp:DataGrid id="DailyGrid" runat="server" OnUpdateCommand="DataGrid_Update"
OnCancelCommand="DataGrid_Cancel" OnEditCommand="DataGrid_Edit" AutoGenerateColumns="False"
BorderStyle="None" GridLines="Vertical" BorderWidth="1px" BorderColor="#999999" BackColor="White" CellPadding="4"
AllowSorting="True">
<FooterStyle forecolor="Black" backcolor="#CCCCCC"></FooterStyle>
<HeaderStyle font-bold="True" forecolor="White" backcolor="#000084"></HeaderStyle>
<PagerStyle horizontalalign="Center" forecolor="Black" backcolor="#999999" mode="NumericPages"></PagerStyle>
<SelectedItemStyle font-bold="True" forecolor="White" backcolor="#008A8C"></SelectedItemStyle>
<AlternatingItemStyle backcolor="Gainsboro"></AlternatingItemStyle>
<ItemStyle forecolor="Black" backcolor="#EEEEEE"></ItemStyle>
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" HeaderText="Action" UpdateText="Update" CancelText="Cancel"
EditText="Edit"></asp:EditCommandColumn>
<asp:BoundColumn DataField="ID" ReadOnly="True" HeaderText="ID"></asp:BoundColumn>
<asp:BoundColumn DataField="Name" HeaderText="Name"></asp:BoundColumn>
<asp:BoundColumn DataField="Address" HeaderText="Address"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
</p>
</form>
</body>
</html>
[edit]formated page width[/edit]
Last edited by a moderator: