I need some help on how to get a value from the gridview. I have the code below:
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If String.Compare(e.CommandName, "Delete") = 0 Then
strSql = String.Format("delete from tblAssignmentDetails where moduleCode= @moduleCode")
conn = New SqlConnection(connectionString)
cmd = New SqlCommand(strSql, conn)
cmd.Parameters.AddWithValue("@moduleCode", I am trying to get the gridview value which is on the first column)
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
void ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="Add")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button clicked
// by the user from the Rows collection.
GridViewRow row = ContactsGridView.Rows[index];
// Create a new ListItem object for the contact in the row.
ListItem item = new ListItem();
item.Text = Server.HtmlDecode(row.Cells[2].Text) + " " + Server.HtmlDecode(row.Cells[3].Text);
// If the contact is not already in the ListBox, add the ListItem
// object to the Items collection of the ListBox control.
if (!ContactsListBox.Items.Contains(item))
{
ContactsListBox.Items.Add(item);
}
}
}
iioannou
Member
67 Points
48 Posts
Gridview get a column value
Jul 10, 2011 03:13 PM|LINK
Hi
I need some help on how to get a value from the gridview. I have the code below:
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If String.Compare(e.CommandName, "Delete") = 0 Then
strSql = String.Format("delete from tblAssignmentDetails where moduleCode= @moduleCode")
conn = New SqlConnection(connectionString)
cmd = New SqlCommand(strSql, conn)
cmd.Parameters.AddWithValue("@moduleCode", I am trying to get the gridview value which is on the first column)
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
End If
End Sub
Please help...
tarunSaini
Contributor
2948 Points
985 Posts
Re: Gridview get a column value
Jul 10, 2011 03:18 PM|LINK
you can use selected index changed by check the check box by show smart tag
iioannou
Member
67 Points
48 Posts
Re: Gridview get a column value
Jul 10, 2011 03:34 PM|LINK
my gridview display some values including the moduleCode value....
But I want to use it in this code:
cmd.Parameters.AddWithValue("@moduleCode", here it should be must code to retrieve the moduleCode value from the gridview)
Zizhuoye Che...
All-Star
21915 Points
1915 Posts
Re: Gridview get a column value
Jul 14, 2011 02:44 AM|LINK
Hi,
You can check these samples for how to access control or text of GridView row in RowCommand event method.
http://ranafaisal.wordpress.com/2008/03/31/how-to-get-the-current-row-in-gridview-row-command-event/
http://worldcode.brinkster.net/CAskQues/find_cnt_grid.aspx
http://aspnetdevelopment.wordpress.com/2009/04/09/aspnet-for-mobiles/
Hope this can help you.
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework
senthilwaits
Contributor
3832 Points
651 Posts
Re: Gridview get a column value
Jul 14, 2011 02:46 AM|LINK
Check this sample.
void ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e) { // If multiple buttons are used in a GridView control, use the // CommandName property to determine which button was clicked. if(e.CommandName=="Add") { // Convert the row index stored in the CommandArgument // property to an Integer. int index = Convert.ToInt32(e.CommandArgument); // Retrieve the row that contains the button clicked // by the user from the Rows collection. GridViewRow row = ContactsGridView.Rows[index]; // Create a new ListItem object for the contact in the row. ListItem item = new ListItem(); item.Text = Server.HtmlDecode(row.Cells[2].Text) + " " + Server.HtmlDecode(row.Cells[3].Text); // If the contact is not already in the ListBox, add the ListItem // object to the Items collection of the ListBox control. if (!ContactsListBox.Items.Contains(item)) { ContactsListBox.Items.Add(item); } } }Senthil Kumar Sundaram