how to pass gridview values to textbox in asp.net using c#? [Resolved]

Posted by Hariinakoti under ASP.NET on 11/28/2012 | Points: 10 | Views : 28738 | Status : [Member] | Replies : 7
Hi All,
how to pass gridview values to textbox in asp.net using c#?

Thanks & Regards
Hari



Responses

Posted by: Hariinakoti on: 11/28/2012 [Member] Starter | Points: 25

Up
0
Down

Resolved
I got Solution:

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
txtid.Enabled = true;
txtid.Text = GridView1.Rows[e.NewEditIndex].Cells[1].Text.ToString();
txtname.Text = GridView1.Rows[e.NewEditIndex].Cells[2].Text.ToString();
txtage.Text = GridView1.Rows[e.NewEditIndex].Cells[3].Text.ToString();
txtaddress.Text = GridView1.Rows[e.NewEditIndex].Cells[4].Text.ToString();
}

Thanks & Regards
Hari

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vivekjj on: 11/28/2012 [Member] Starter | Points: 25

Up
0
Down
its working very nice

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vivekjj on: 11/28/2012 [Member] Starter | Points: 25

Up
0
Down
Hi its working but when AutoGenerateColumns="true" if false means its not working . tel me, what is the way to solve it?

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sandhyab on: 11/29/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,See this.Here when you click edit Update and cancel links will come. This updateRow is your requirement. i.e, Populate grid vlues to twxt box
  <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 

AutoGenerateColumns="false" OnRowEditing="EditRow" OnRowCancelingEdit="CancelEditRow"
OnRowUpdating="UpdateRow" DataKeyNames="AutoId" OnRowDeleting="DeleteRow" AllowPaging="true"
PageSize="2" OnPageIndexChanging="ChangePage" AllowSorting="true" OnSorting="SortRecords">


Code behind:
Page Load:
 protected void Page_Load(object sender, EventArgs e)

{
if (!IsPostBack)
{
PopulateData();
}
}

PopulateData:

 private void PopulateData()

{
DataTable table = new DataTable();
using (SqlConnection conn = new SqlConnection(_connStr))
{
string sql = "Select * from PersonDetails";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
ad.Fill(table);
}
}
}
GridView1.DataSource = table;
GridView1.DataBind();
}
Edit Row:
protected void EditRow(object sender, GridViewEditEventArgs e)

{
GridView1.EditIndex = e.NewEditIndex;
this.PopulateData();
}


CanceEditRow:
protected void CancelEditRow(object sender, GridViewCancelEditEventArgs e)

{
GridView1.EditIndex = -1;
this.PopulateData();
}


DEleteRow:
protected void DeleteRow(object sender, GridViewDeleteEventArgs e)

{
var autoID = GridView1.DataKeys[e.RowIndex].Value;
using (SqlConnection conn = new SqlConnection(_connStr))
{
string sql = "Delete from PersonDetails" + " where AutoId = @AutoId";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@AutoId", autoID);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
lblMessage.Text = "Record has been deleted successfully !";
lblMessage.ForeColor = System.Drawing.Color.Red;
this.PopulateData();
}

UpdateRow:

 protected void UpdateRow(object sendedr, GridViewUpdateEventArgs e)

{
var autoID = GridView1.DataKeys[e.RowIndex].Value;
GridViewRow row = GridView1.Rows[e.RowIndex] as GridViewRow;
TextBox tFirstName = row.FindControl("txtFirstName") as TextBox;
TextBox tLastName = row.FindControl("txtLastName") as TextBox;
TextBox tAge = row.FindControl("txtAge") as TextBox;
DropDownList dropActive = row.FindControl("dropActive") as DropDownList;
using (SqlConnection conn = new SqlConnection(_connStr))
{
string sql = "Update PersonDetails set FirstName = @FirstName, LastName=@LastName, Age= @Age, Active = @Active" +
" where AutoId = @AutoId";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@FirstName", tFirstName.Text.Trim());
cmd.Parameters.AddWithValue("@LastName", tLastName.Text.Trim());
cmd.Parameters.AddWithValue("@Age", tAge.Text.Trim());
cmd.Parameters.AddWithValue("@Active", dropActive.SelectedValue);
cmd.Parameters.AddWithValue("@AutoId", autoID);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
lblMessage.Text = "Record updated successfully !";
GridView1.EditIndex = -1;
this.PopulateData();
}



Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vivekjj on: 11/29/2012 [Member] Starter | Points: 25

Up
0
Down
<asp:GridView ID="grdshow" runat="server" Width="100%" AutoGenerateColumns="false" 

AutoGenerateEditButton="True" onrowediting="grdshow_RowEditing">
<Columns>
<asp:TemplateField HeaderText="uniqueid" Visible="true">
<ItemTemplate>
<div align="left">
<asp:LinkButton ID="lnkbtn" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "uniqueid") %>' OnClick="lnk_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<div align="left"><asp:Label ID="lblname" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "name") %>' ></asp:Label></div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password">
<ItemTemplate>
<div align="left"><asp:Label ID="lblpwd" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "password") %>' ></asp:Label></div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<div align="left"><asp:Label ID="lblemail" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "email") %>' ></asp:Label></div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile">
<ItemTemplate>
<div align="left"><asp:Label ID="lblmob" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "mobile") %>' ></asp:Label></div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


i am using this code instead of onrowediting="grdshow_RowEditing" i need to edit by linkbutton tat means while click the linkbutton in grid the particular ROW will be need Edit

i hav used following code
  protected void lnk_Click(object sender, EventArgs e)

{
lblunique.Text = grdshow.Rows[0].Cells[1].Text.ToString();
txtname.Text = grdshow.Rows[0].Cells[1].Text.ToString();
txtpwd.Text = grdshow.Rows[0].Cells[1].Text.ToString();
txtemail.Text = grdshow.Rows[0].Cells[1].Text.ToString();
txtmob.Text =grdshow.Rows[0].Cells[1].Text.ToString();
}


whether its correct

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 1/24/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Display_selected_records._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateSelectButton="true" AutoGenerateColumns="true"
BackColor="#CCFFCC" Font-Bold="False" ForeColor="Maroon"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<AlternatingRowStyle BackColor="#FFCC66" />
</asp:GridView>
<br />
<asp:Label ID="lb1" runat="server" Text="ID : " Font-Bold="True" Width="100px"></asp:Label>
<asp:Label ID="lbl_id" runat="server" ForeColor="#CC3300"></asp:Label>
<br />
<br />
<asp:Label ID="lb3" runat="server" Text="NAME : " Font-Bold="True"
Width="100px"></asp:Label>
<asp:Label ID="lbl_name" runat="server" ForeColor="#CC3300"></asp:Label>
<br />
<br />
<asp:Label ID="lb5" runat="server" Text="ADDRESS : " Font-Bold="True"
Width="100px"></asp:Label>
<asp:Label ID="lbl_address" runat="server" ForeColor="#CC3300"></asp:Label>
<br />
<br />
<asp:Label ID="lb7" runat="server" Text="MARK : " Font-Bold="True"
Width="100px"></asp:Label>
<asp:Label ID="lbl_marks" runat="server" ForeColor="#CC3300"></asp:Label>
<br />
<br />
<asp:Label ID="lb9" runat="server" Text="YEAR : " Font-Bold="True"
Width="100px"></asp:Label>
<asp:Label ID="lbl_year" runat="server" ForeColor="#CC3300"></asp:Label>
</div>
</form>
</body>
</html>



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 Display_selected_records
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
lbl_id.Text = GridView1.SelectedRow.Cells[1].Text;
lbl_name.Text = GridView1.SelectedRow.Cells[2].Text;
lbl_address.Text = GridView1.SelectedRow.Cells[3].Text;
lbl_marks.Text = GridView1.SelectedRow.Cells[4].Text;
lbl_year.Text = GridView1.SelectedRow.Cells[5].Text;
}

void bindgrid()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from student";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "student");
GridView1.DataSource = ds;
GridView1.DataMember = "student";
GridView1.DataBind();
con.Close();
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgrid();
}
}
}
}


If this post helps you mark it as answer
Thanks

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 1/24/2013 [Member] Starter | Points: 25

Up
0
Down
use this and change accordng to ur need

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)

{
myobj.myconnection();
string query = "select * from regional where ID='"+GridView1.SelectedValue.ToString()+"'";
SqlCommand cmd = new SqlCommand(query , myobj .sqlcon );
SqlDataReader sqldr = cmd.ExecuteReader();
sqldr.Read();
if (sqldr.HasRows)
{
txtreg.Text = sqldr["regional"].ToString();
DropDownList1.Text = sqldr["stateid"].ToString();


}
else
{
txtreg.Text = "";
DropDownList1.ClearSelection();
}

}


Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response