Usually, when I do this I add a CheckBox to each row of the GridView. Then when the user wishes to to a mass update, I test the CheckBox and update that row, if the CheckBox is checked. What I do is go through the GridView and if the CheckBox is checked,
I pull the row's DataKey, then use this key to update the row.
Dim SQLStmt As String = "Update myTable SET columnName = '" & txtNewColumnData.text & "' Where Key IN("
For I = 0 To GridView1.Rows.Count - 1
If DirectCast(GridView1.Rows(I).FindControl("cbUpdateThisRecord"), CheckBox).Checked = True Then
If Not IsDBNull(GridView1.DataKeys(I).Values(0)) Then
CID = GridView1.DataKeys(I).Values(0)
' Here you could update the single Row based on the Key
UpdateRow(CID)
' Or you could create an SQL Statement to update multiple rows
SQLStmt += CID.ToString & ", "
End If
End If
Next
' SQLStmt would update multiple rows
SQLStmt = SQLStmt.Substring(0, (SQLStmt.Length - 2)) & ")"
I understand this but what i am trying to do is make a column editable.
Imagine a gridview with a button outside of it that says "edit" and when you click this is changes every row within the gridview to the edit template, the edit button has now changed to save and can allow the code behind to do a foreach to save the rows.
Well bearing this in mind, i just want to put the edititemtemplate on one column so when the edit button is clicked it enables edit mode within that column on every row. This is what i am after.
Ok, that solution wasn't close. How about converting the column to a TemplateField. Then changing the Label to a TextBox, then reading through the GridView row by row and updating (This does use a CheckBox as explained above):
Private Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim CID As Integer
For I = 0 To GridView1.Rows.Count - 1
If DirectCast(GridView1.Rows(I).FindControl("cbUpdateThisRecord"), CheckBox).Checked = True Then
If Not IsDBNull(GridView1.DataKeys(I).Values(0)) Then
CID = GridView1.DataKeys(I).Values(0)
Dim NewData As String = DirectCast(GridView1.Rows(I).FindControl("TextBox1"), TextBox).Text
UpdateRowColumn(CID, NewData)
End If
End If
Next
End Sub
Protected Sub UpdateRowColumn(ByVal CID, ByVal NewData)
Dim connString As String = ConfigurationManager.ConnectionStrings("ForumConnectionString1").ConnectionString.ToString
Dim ProvidrName As String = ConfigurationManager.ConnectionStrings("ForumConnectionString1").ProviderName
Dim SQLStmt As String = "Update contact set GroupId='" & NewData & "' Where ContactId=" & CID.ToString
Using SQLConn As New SqlConnection(connString)
Using SQLcmd As New SqlCommand(SQLStmt, SQLConn)
SQLcmd.Connection = SQLConn
SQLcmd.CommandType = CommandType.Text
SQLcmd.CommandText = SQLStmt
SQLConn.Open()
SQLcmd.ExecuteNonQuery()
End Using
End Using
End Sub
connersz
Member
153 Points
175 Posts
Edit just one column in gridview
Aug 12, 2011 01:19 PM|LINK
Rather than edit something in a row of the gridview i would like to edit a whole column at once and then click save.
Obviously the buttons would need to be outside the gridview, does anyone know how to do this as i cant find out online?
shahed.kazi
All-Star
17953 Points
3635 Posts
Re: Edit just one column in gridview
Aug 12, 2011 01:28 PM|LINK
I don't think that would work. Even if you get the interface right to edit columns, in the database end, the chnages will be committed on a row basis.
.NET World |Captcha Control
connersz
Member
153 Points
175 Posts
Re: Edit just one column in gridview
Aug 12, 2011 02:25 PM|LINK
Yeh but as the amount of rows will never be more than 12 i thought i could do a foreach to save them.
Md. Shahinur...
Member
516 Points
91 Posts
Re: Edit just one column in gridview
Aug 12, 2011 02:29 PM|LINK
Dear,
You must use a foreach loop in Save button event. Each insert method call inside a foreach method.
foreach (GridViewRow gvr in GridView1.Rows)
{
string str1 = gvr.Cells[colunmIndex].Text;
//Your code here
object a = new object();
a.name = str1;
BLL.Insert(a);
}
Shahinur Islam
Software Engineer
paindaasp
Star
12060 Points
2034 Posts
Re: Edit just one column in gridview
Aug 12, 2011 02:32 PM|LINK
Usually, when I do this I add a CheckBox to each row of the GridView. Then when the user wishes to to a mass update, I test the CheckBox and update that row, if the CheckBox is checked. What I do is go through the GridView and if the CheckBox is checked, I pull the row's DataKey, then use this key to update the row.
Dim SQLStmt As String = "Update myTable SET columnName = '" & txtNewColumnData.text & "' Where Key IN(" For I = 0 To GridView1.Rows.Count - 1 If DirectCast(GridView1.Rows(I).FindControl("cbUpdateThisRecord"), CheckBox).Checked = True Then If Not IsDBNull(GridView1.DataKeys(I).Values(0)) Then CID = GridView1.DataKeys(I).Values(0) ' Here you could update the single Row based on the Key UpdateRow(CID) ' Or you could create an SQL Statement to update multiple rows SQLStmt += CID.ToString & ", " End If End If Next ' SQLStmt would update multiple rows SQLStmt = SQLStmt.Substring(0, (SQLStmt.Length - 2)) & ")"Note: This code HAS NOT BEEN TESTED!
connersz
Member
153 Points
175 Posts
Re: Edit just one column in gridview
Aug 12, 2011 02:56 PM|LINK
I understand this but what i am trying to do is make a column editable.
Imagine a gridview with a button outside of it that says "edit" and when you click this is changes every row within the gridview to the edit template, the edit button has now changed to save and can allow the code behind to do a foreach to save the rows.
Well bearing this in mind, i just want to put the edititemtemplate on one column so when the edit button is clicked it enables edit mode within that column on every row. This is what i am after.
konanki
Contributor
6198 Points
1239 Posts
Re: Edit just one column in gridview
Aug 12, 2011 03:40 PM|LINK
Hi,
which ever column you don't need to editable use label, for other columns use textbox which will editable.
<EditItemTemplate>
<asp:Label ID="lblsno" Text='<%# Eval("Sno") %>' runat="server"></asp:Label>
</EditItemTemplate>
editfield
<EditItemTemplate>
<asp:TextBox ID="txtsna" Text='<%# Eval("sna") %>' runat="server"></asp:TextBox>
</EditItemTemplate>
paindaasp
Star
12060 Points
2034 Posts
Re: Edit just one column in gridview
Aug 12, 2011 03:43 PM|LINK
Ok, that solution wasn't close. How about converting the column to a TemplateField. Then changing the Label to a TextBox, then reading through the GridView row by row and updating (This does use a CheckBox as explained above):
Private Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdate.Click Dim CID As Integer For I = 0 To GridView1.Rows.Count - 1 If DirectCast(GridView1.Rows(I).FindControl("cbUpdateThisRecord"), CheckBox).Checked = True Then If Not IsDBNull(GridView1.DataKeys(I).Values(0)) Then CID = GridView1.DataKeys(I).Values(0) Dim NewData As String = DirectCast(GridView1.Rows(I).FindControl("TextBox1"), TextBox).Text UpdateRowColumn(CID, NewData) End If End If Next End Sub Protected Sub UpdateRowColumn(ByVal CID, ByVal NewData) Dim connString As String = ConfigurationManager.ConnectionStrings("ForumConnectionString1").ConnectionString.ToString Dim ProvidrName As String = ConfigurationManager.ConnectionStrings("ForumConnectionString1").ProviderName Dim SQLStmt As String = "Update contact set GroupId='" & NewData & "' Where ContactId=" & CID.ToString Using SQLConn As New SqlConnection(connString) Using SQLcmd As New SqlCommand(SQLStmt, SQLConn) SQLcmd.Connection = SQLConn SQLcmd.CommandType = CommandType.Text SQLcmd.CommandText = SQLStmt SQLConn.Open() SQLcmd.ExecuteNonQuery() End Using End Using End Sub