how to show data on a next form
On a from a text box with a button and a gridview i placed. Behind button i have done search query work. Which is working very fine and rendering data into my gridview. Now, i want.... As user click to any record of my gridview then, that specific record will show in another form within different textboxes plus with a gridview. or or user click on a button to view, detail view of the record that detail view will be shown on diffrent textboxe in a diffrent form really sorry i am not native speaker of english therefore, ignore my language mistakes;(
Nadeem ShaikPosted Feb 12, 2013, 2:27 AM
form1 contains , Datagridview1 and button1
datagridview1 consists of single record with columns like
Name Address Age
abcd xyz 20
form 2 consists of three textboxes(for Name, Address, Age) and Datagridview2
Q: As user click to any record of my gridview then, that specific record will show in another form within different textboxes plus with a gridview.
Sol :
initially change the every controls of both forms modifier to public, and disable(Enabled = false) the button of form1
in form1 -->
Declare string variables
public string Name;
public string Address;
public int Age;
Datagridview1_CellClick Event
write the below code
Name = Datagridview1.Rows[e.Rowindex].Cells[0].value.ToString();
Address = Datagridview1.Rows[e.Rowindex].Cells[1].value.ToString();
Age = Convert.ToInt32(Datagridview1.Rows[e.Rowindex].Cells[2].value.ToString());
//enable the button and change the button text to view
Button1.Enabled = true;
Button1.Text = "VIEW";
In Button1_Click Event
write the below code
form2 f2 = new from2();
f2.tetxtbox1.text = Name;
f2.tetxtbox2.text = Address;
f2.tetxtbox3.text = Convert.ToString(Age);
//it stores the single record in form2 datagridview zeroth row
f2.Datagridview2.Rows[0].Cells[0].Value = Name;
f2.Datagridview2.Rows[0].Cells[1].Value = Address;
f2.Datagridview2.Rows[0].Cells[2].Value = Convert.ToString(Age);
f2.Show();
this.Close();
Faizan KhanPosted Feb 7, 2013, 1:41 AM
Satyapriya NayakPosted Feb 7, 2013, 12:02 AM
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test1.aspx.cs" Inherits="Show_data_on_a_next_form.test1" %>
test1.aspx.cs
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;
namespace Show_data_on_a_next_form
{
public partial class test1 : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgrid();
}
}
private void bindgrid()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from employee";
com = new SqlCommand(str, con);
SqlDataReader reader;
reader = com.ExecuteReader();
g1.DataSource = reader;
g1.DataBind();
con.Close();
}
protected void btn_search_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from employee where empname = '" + TextBox1.Text + "'";
//str = "select * from employee where empname like '" + TextBox1.Text + "%'";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "employee");
con.Close();
g1.DataSource = ds;
g1.DataMember = "employee";
g1.DataBind();
}
}
}
test2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test2.aspx.cs" Inherits="Show_data_on_a_next_form.test2" %>
test2.aspx.cs
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;
namespace Show_data_on_a_next_form
{
public partial class test2 : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
string s1;
SqlCommand com;
protected void Page_Load(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
s1 = Request.QueryString[0];
str = "select * from employee where empid='" + s1 + "'";
com = new SqlCommand(str, con);
SqlDataReader reader;
reader = com.ExecuteReader();
if (reader.Read())
{
Label1.Text = reader["empname"].ToString();
Label2.Text = reader["empaddress"].ToString();
Label3.Text = reader["empsal"].ToString();
Label4.Text = reader["empphone"].ToString();
Label5.Text = reader["empfax"].ToString();
Label6.Text = reader["empcity"].ToString();
Label7.Text = reader["empstate"].ToString();
Label8.Text = reader["empzip"].ToString();
Label9.Text = reader["emplic"].ToString();
Label10.Text = reader["empstatus"].ToString();
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
}