I'm trying to build a datagrid which lets you update multiple rows at once. I'm able to display my data in the grid and, using templates, allow the user to edit the data in the grid. I'm lost on how I access the data in the grid afterwards. To test, I am simply trying to loop through the datagrid and output a field to a variable. If I'm able to get this data to the variable then I'm confident I can get it back into the database.
My code below is a test program. I have a table with a location code and store description in each row. First I display the data and then let the user change the descriptions. Following, the user clicks a button and I'm hoping to capture the last cell in the grid.
Any help you can provide is greatly appreciated - deadline is only a week away.
Thanks,
Steve
<%@ Page Language="VB" %>
<script runat="server">
' Insert page code here
'
sub bind()
Datagrid1.DataSource = Select_dsstores()
datagrid1.DataBind()
end sub
Function select_dsstores() As System.Data.DataSet
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=d:\2004 Orders."& _
"mdb"
Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)
Dim queryString As String = "SELECT [travisstores].[ST_KEY], [travisstores].[ST_DESC] FROM [travisstores]"
Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection
Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
dataAdapter.SelectCommand = dbCommand
Dim dataSet As System.Data.DataSet = New System.Data.DataSet
dataAdapter.Fill(dataSet)
Return dataSet
End Function
'this is where I get lost. I'm trying to figure out how to take the data
'from the datagrid and put it anywhere else. Obviously my final goal is to update
'back to the database but for now I would be happy getting the last row's edits
'into a variable
Function updatecity()
Dim di As datagriditem
dim dr as system.data.datarow
Dim strSQL As String
dim city as string
For Each dr In datagrid1.tables(0).rows
city = CType(dr.FindControl("st_desc"
, TextBox).Text
next
label1.text= "new city: " & city
end function
Sub Button1_Click(sender As Object, e As EventArgs)
updatecity()
End Sub
Sub Button2_Click(sender As Object, e As EventArgs)
bind()
End Sub
End Sub
</script>
<html>
<head>
</head>
<body>
<form id="Form1" method="post" runat="server">
<asp
ataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" Datakeyfield="st_key">
<Columns>
<asp:TemplateColumn SortExpression="Locn" HeaderText="Locn">
<ItemTemplate>
<asp:TextBox id="Locn" runat="server" Width="109px" Text='<%# DataBinder.Eval(Container, "DataItem.st_key"
%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn SortExpression="City" HeaderText="City">
<ItemTemplate>
<asp:TextBox id="City" runat="server" Width="109px" Text='<%# DataBinder.Eval(Container, "DataItem.st_Desc"
%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle horizontalalign="Center" forecolor="DarkSlateBlue" backcolor="PaleGoldenrod"></PagerStyle>
</asp
ataGrid>
<p>
<asp:Label id="Label1" runat="server">Label</asp:Label>
</p>
<p>
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="check"></asp:Button>
<br />
<asp:Button id="Button2" onclick="Button2_Click" runat="server" Text="Bind"></asp:Button>
</p>
</form>
</body>
</html>
My code below is a test program. I have a table with a location code and store description in each row. First I display the data and then let the user change the descriptions. Following, the user clicks a button and I'm hoping to capture the last cell in the grid.
Any help you can provide is greatly appreciated - deadline is only a week away.
Thanks,
Steve
<%@ Page Language="VB" %>
<script runat="server">
' Insert page code here
'
sub bind()
Datagrid1.DataSource = Select_dsstores()
datagrid1.DataBind()
end sub
Function select_dsstores() As System.Data.DataSet
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=d:\2004 Orders."& _
"mdb"
Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)
Dim queryString As String = "SELECT [travisstores].[ST_KEY], [travisstores].[ST_DESC] FROM [travisstores]"
Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection
Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
dataAdapter.SelectCommand = dbCommand
Dim dataSet As System.Data.DataSet = New System.Data.DataSet
dataAdapter.Fill(dataSet)
Return dataSet
End Function
'this is where I get lost. I'm trying to figure out how to take the data
'from the datagrid and put it anywhere else. Obviously my final goal is to update
'back to the database but for now I would be happy getting the last row's edits
'into a variable
Function updatecity()
Dim di As datagriditem
dim dr as system.data.datarow
Dim strSQL As String
dim city as string
For Each dr In datagrid1.tables(0).rows
city = CType(dr.FindControl("st_desc"
next
label1.text= "new city: " & city
end function
Sub Button1_Click(sender As Object, e As EventArgs)
updatecity()
End Sub
Sub Button2_Click(sender As Object, e As EventArgs)
bind()
End Sub
End Sub
</script>
<html>
<head>
</head>
<body>
<form id="Form1" method="post" runat="server">
<asp
<Columns>
<asp:TemplateColumn SortExpression="Locn" HeaderText="Locn">
<ItemTemplate>
<asp:TextBox id="Locn" runat="server" Width="109px" Text='<%# DataBinder.Eval(Container, "DataItem.st_key"
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn SortExpression="City" HeaderText="City">
<ItemTemplate>
<asp:TextBox id="City" runat="server" Width="109px" Text='<%# DataBinder.Eval(Container, "DataItem.st_Desc"
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle horizontalalign="Center" forecolor="DarkSlateBlue" backcolor="PaleGoldenrod"></PagerStyle>
</asp
<p>
<asp:Label id="Label1" runat="server">Label</asp:Label>
</p>
<p>
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="check"></asp:Button>
<br />
<asp:Button id="Button2" onclick="Button2_Click" runat="server" Text="Bind"></asp:Button>
</p>
</form>
</body>
</html>