sny23vpb
asked on
RowEditing Event Code
Hello:
I'm trying to create a dynamic data command and data-adapter to populate a grid similar to the code below with autogenerateeditbutton='tr ue'.
I know I need to populate the GridView1_RowEditing event but am unsure how to do that.
Several tries have been unsuccessful.
I'm trying to keep everything dynamic and do not know if I need to put my updatecommand in that event to make it workl ?
The table in the sql below could be choosen as a different table so in keeping everything dynamic I believe I'm going to have to populate the updatecommand in the rowediting event as something like update testtable set field1=@field1 where ...... and I'm not sure how to write that update process so it updates the row they are on when they click edit.
Its running in asp.net
Thank you for any help.
-----------
Dim SQL_String As String = "select * from testtable"
Dim connection As Data.SqlClient.SqlConnecti on
connection = New Data.SqlClient.SqlConnecti on("server =USFTW007F LN3G1\SQLE XPRESS;Int egrated Security=SSPI;Initial Catalog=test")
connection.Open()
Dim cmd As New Data.SqlClient.SqlCommand
Dim ds As New Data.DataSet
Dim dataadapter As Data.SqlClient.SqlDataAdap ter
cmd.Connection = connection
cmd.CommandText = SQL_String
cmd.CommandType = Data.CommandType.Text
Try
dataadapter = New Data.SqlClient.SqlDataAdap ter(cmd)
dataadapter.Fill(ds)
cmd.Parameters.Clear()
Finally
End Try
connection.Close()
GridView1.DataSource = ds
GridView1.DataBind()
-------------------------
I'm trying to create a dynamic data command and data-adapter to populate a grid similar to the code below with autogenerateeditbutton='tr
I know I need to populate the GridView1_RowEditing event but am unsure how to do that.
Several tries have been unsuccessful.
I'm trying to keep everything dynamic and do not know if I need to put my updatecommand in that event to make it workl ?
The table in the sql below could be choosen as a different table so in keeping everything dynamic I believe I'm going to have to populate the updatecommand in the rowediting event as something like update testtable set field1=@field1 where ...... and I'm not sure how to write that update process so it updates the row they are on when they click edit.
Its running in asp.net
Thank you for any help.
-----------
Dim SQL_String As String = "select * from testtable"
Dim connection As Data.SqlClient.SqlConnecti
connection = New Data.SqlClient.SqlConnecti
connection.Open()
Dim cmd As New Data.SqlClient.SqlCommand
Dim ds As New Data.DataSet
Dim dataadapter As Data.SqlClient.SqlDataAdap
cmd.Connection = connection
cmd.CommandText = SQL_String
cmd.CommandType = Data.CommandType.Text
Try
dataadapter = New Data.SqlClient.SqlDataAdap
dataadapter.Fill(ds)
cmd.Parameters.Clear()
Finally
End Try
connection.Close()
GridView1.DataSource = ds
GridView1.DataBind()
-------------------------
ASKER
ok. Thanks.
So; if I have to use custom editing because I want my grid to be based on the table selected in a drop-down; what is the basic syntax for me to express the update of the field with the value from the grid which I am guessing is identified by e.RowIndex.
And do I need to populate this in both the RowUpdating and RowEditing events ?
Since mine is so dynamic; I guess I"m going to have to pull down each field based on the dropdown table selected using a query against sysobjects.
I wish I could keep the standard edit functionality without having to define everything but I'm not sure how to get the system to auto-generate the updatecommand and deletecommand when the table is changed on the sqlconnection by a dropdown.
So; if I have to use custom editing because I want my grid to be based on the table selected in a drop-down; what is the basic syntax for me to express the update of the field with the value from the grid which I am guessing is identified by e.RowIndex.
And do I need to populate this in both the RowUpdating and RowEditing events ?
Since mine is so dynamic; I guess I"m going to have to pull down each field based on the dropdown table selected using a query against sysobjects.
I wish I could keep the standard edit functionality without having to define everything but I'm not sure how to get the system to auto-generate the updatecommand and deletecommand when the table is changed on the sqlconnection by a dropdown.
Understand that the auto generate Update, Delete, and Insert statements generated by the wizard are created at design time, they are not generated at run time. Executing stored procedures based on the table selected would likely be best. Alternatively you could store the SQL statements in your code behind, one for each command, for each table. Obviously, this would be a problem if the number tables you wish to access is an in-determinate number. In that case, you would have to write a procedure to generate an SQL statement based on the table definition.
Couple questions, how do you want to handle or store the necessary SQL statements? Also, are you making available a set number of tables for viewing or will that number also be dynamic?
Couple questions, how do you want to handle or store the necessary SQL statements? Also, are you making available a set number of tables for viewing or will that number also be dynamic?
ASKER
Thanks. There are many tables. And it will be best if the system will automatically handle new tables in the sysobjects table without further intervention.
The only way I can think of to do that would be to write the updatecommand to dynamically determine the fields and parameters which I think would just be something like
sqldatasource.updatecomman d="UPDATE [temp_tbl] SET [task] = @task WHERE [ID] = @original_ID AND [task] = @original_task"
Then. Either the GridView1_RowUpdating or GridView1_RowEditing will have to somehow reference the row your on with code like: ("TASK")= gridview1.task ; but I"m not certain of how this syntax should work and have not located any good samples.
I presume I would do likewise for delete; but either use the detailsview for the insert process or dynamically create a union query to create a blank row for inserts off the grid.
I'm increasing points for all your trouble; please forward any thoughts you may have on how the syntax for the grid-updating event might work as well as anything you see that I'm missing. All the examples I've seen are just not applicable.
The only way I can think of to do that would be to write the updatecommand to dynamically determine the fields and parameters which I think would just be something like
sqldatasource.updatecomman
Then. Either the GridView1_RowUpdating or GridView1_RowEditing will have to somehow reference the row your on with code like: ("TASK")= gridview1.task ; but I"m not certain of how this syntax should work and have not located any good samples.
I presume I would do likewise for delete; but either use the detailsview for the insert process or dynamically create a union query to create a blank row for inserts off the grid.
I'm increasing points for all your trouble; please forward any thoughts you may have on how the syntax for the grid-updating event might work as well as anything you see that I'm missing. All the examples I've seen are just not applicable.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Open in new window