Click here to Skip to main content
16,016,501 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have program in asp.net which displays product details in gridview. If I click on the select button on the gridview that row items will select and added to the corresponding textboxes. But n0w I have a submit button. So I want to add the selected index changed event to that submit button. pls help.

this is my code in gridview selected index changed:
CSS
GridViewRow row = GridView1.SelectedRow;
        TextBox1.Text = row.Cells[1].Text;
        TextBox2.Text = row.Cells[2].Text;
        TextBox3.Text = row.Cells[3].Text;
        TextBox4.Text = row.Cells[4].Text;



how can I add this to the submit button click event.

Added from answering:
C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using System.Collections.Generic;

public partial class Default4 : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data source = VISHNUIT;Initial Catalog = sims1; Integrated security= True");
    SqlCommand cmd = new SqlCommand();
    SqlDataReader dr;
    DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
    {
        Body.Attributes["bgcolor"] = "apple green"; 
    }
   protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = GridView1.SelectedRow;
        TextBox3.Text = row.Cells[1].Text;
        TextBox4.Text = row.Cells[2].Text;
        TextBox5.Text = row.Cells[3].Text;
    }
    protected void TextBox3_TextChanged(object sender, EventArgs e)
    {
        String str = "select * from login where (uname like  + @search + '%' )";
        SqlCommand xp = new SqlCommand(str,con);
        xp.Parameters.Add("@search", SqlDbType.VarChar).Value = TextBox3.Text;
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = xp;
        DataSet ds = new DataSet();
        da.Fill(ds, "uname");
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        
    }
}


Here button1 is my submit button.
Posted
Updated 19-Jan-15 0:13am
v2
Comments
Jeet Gupta 19-Jan-15 3:59am    
post your full code so can understand the problem
Member 11357862 19-Jan-15 4:17am    
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using System.Collections.Generic;

public partial class Default4 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data source = VISHNUIT;Initial Catalog = sims1; Integrated security= True");
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
Body.Attributes["bgcolor"] = "apple green";
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
TextBox3.Text = row.Cells[1].Text;
TextBox4.Text = row.Cells[2].Text;
TextBox5.Text = row.Cells[3].Text;
}
protected void TextBox3_TextChanged(object sender, EventArgs e)
{
String str = "select * from login where (uname like + @search + '%' )";
SqlCommand xp = new SqlCommand(str,con);
xp.Parameters.Add("@search", SqlDbType.VarChar).Value = TextBox3.Text;
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = xp;
DataSet ds = new DataSet();
da.Fill(ds, "uname");
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{

}
}

Here button1 is my submit button.
HariPrasad katakam 19-Jan-15 5:03am    
Where is your submit button located (is it inside gridview)? What is your exact requirement for submit button?
Member 11357862 19-Jan-15 5:22am    
no its outside the gridview,
I need to add the selected indexchanged event on this button. If I click on the submit button(btn1) then this code want to execute
GridViewRow row = GridView1.SelectedRow;
TextBox3.Text = row.Cells[1].Text;
TextBox4.Text = row.Cells[2].Text;
TextBox5.Text = row.Cells[3].Text;
[no name] 19-Jan-15 7:23am    
If you want to get the row value in the button click tell me how you will determine the row values? If multiple rows will be there then which row you want to select on the button click?

if you only want to called the gridview selected index change property on button click then just simply do one thing..
write below code on button click.


C#
protected void Button2_Click(object sender, EventArgs e)
{
      GridView1_SelectedIndexChanged(sender, e)

}


it just called the gridview1_selectedIndexchanged event in your button click event. then all your code written in the index changed event will fire. hope this answer helps you.
 
Share this answer
 
Comments
Member 11357862 19-Jan-15 6:46am    
I hv used the code, but it shows error her
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
TextBox3.Text = row.Cells[1].Text;
TextBox4.Text = row.Cells[2].Text;
TextBox5.Text = row.Cells[3].Text;
}
protected void Button2_Click(object sender, EventArgs e)
{
GridView1_SelectedIndexChanged(sender, e)

}
error:null reference exception was unhandled by user code
shows this error on TextBox3.Text = row.Cells[1].Text;
Jeet Gupta 19-Jan-15 22:28pm    
i understand your problem. i think this error is coming because when you click on the submit button or Button2 no gridview is selected and thats why gridview selectedIndexchanged when fired gives the error of null reference becuase its not finding which row in gridview selected index is fired.
So to solve this. you can do two things for selecting the gridview. either use a checkbox/RadioButton in gridview to select a particular row and then click on submit button and then find the selected row of radio button or else you can add a button in gridview and do the same thing..
you can check these two links given below. hope that will solve your issue.

http://www.aspsnippets.com/Articles/Get-selected-row-in-GridView-on-Button-Click-in-ASPNet.aspx

http://www.aspdotnet-suresh.com/2014/08/get-aspnet-gridview-selected-row-values-on-button-click-in-csharp.html
Member 11357862 20-Jan-15 0:18am    
thank u Jeet
CSS
GridViewRow row = GridView1.SelectedRow;
        TextBox1.Text = row.Cells[1].Text;
        TextBox2.Text = row.Cells[2].Text;
        TextBox3.Text = row.Cells[3].Text;
        TextBox4.Text = row.Cells[4].Text;




store selected row index in hidden field
ex: hiddenfield1.value=0;


XML
TextBox1.Text = GridView1.Rows[hiddenfield1.value].Cells[1].Text;
        TextBox2.Text = GridView1.Rows[hiddenfield1.value].Cells[2].Text;
        TextBox3.Text = GridView1.Rows[hiddenfield1.value].Cells[3].Text;
        TextBox4.Text = GridView1.Rows[hiddenfield1.value].Cells[4].Text;
</pre>
.rows[
 
Share this answer
 
Comments
Member 11357862 19-Jan-15 5:52am    
protected void Button2_Click(object sender, EventArgs e)
{
TextBox3.Text = GridView1.Rows[HiddenField1.Value].Cells[1].Text;
TextBox4.Text = GridView1.Rows[HiddenField1.Value].Cells[2].Text;
TextBox5.Text = GridView1.Rows[HiddenField1.Value].Cells[3].Text;
TextBox6.Text = GridView1.Rows[HiddenField1.Value].Cells[4].Text;

}
I have added this on button1, but it shows error. How can I declare the hidden field value?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900