Click here to Skip to main content
16,012,316 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
XML
i have a Grid View

<pre lang="HTML">
<asp:GridView AutoGenerateColumns="False" ID="GView" runat="server" CellPadding="4"
                            ForeColor="#333333" GridLines="Horizontal" Width="100%"
                            OnRowEditing="GView_RowEditing">
<Columns>
                                <asp:BoundField DataField="UID" HeaderText="ID" Visible="false" />
                                <asp:TemplateField HeaderText="User Name" ItemStyle-CssClass="gv-item1">
                                    <EditItemTemplate>
                                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("UFName") %>'></asp:TextBox>
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("UFName") %>'></asp:Label>
                                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("UMName") %>'></asp:Label>
                                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("ULName") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:BoundField DataField="UNic" HeaderText="N.I.C" ItemStyle-CssClass="gv-item2"/>

                                <asp:CommandField ShowEditButton="true" />

                            </Columns>
                        </asp:GridView>

</pre>

i want to display the records showing in the gridview webform. but when ever i click over the edit button its always load first record. e.g i click over the edit button in print of 2nd record it will load the first record into the webform.
Posted
Comments
Altaful Haq 4-Aug-15 9:00am    
C# Coding is Here

protected void GView_RowEditing(object sender, GridViewEditEventArgs e)
{
Form_Enable();


using (SqlConnection conn = new SqlConnection(conStr))
using (SqlCommand cmd = new SqlCommand(@"SELECT * FROM UserInfo WHERE UID = UID", conn))
{
conn.Open();
using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
if (dr.Read())
{
// int id = Convert.ToInt32(dr["UID"]);
// txtUID.Text = (id + 1).ToString();
txtUID.Text = Convert.ToString(dr["UID"]);

txtFName.Text = Convert.ToString(dr["UFName"]); txtMName.Text = Convert.ToString(dr["UMName"]);
txtLName.Text = Convert.ToString(dr["ULName"]); txtUNic.Text = Convert.ToString(dr["UNic"]);
txtUDob.Text = Convert.ToString(dr["UDOB"]); txtUHPhone.Text = Convert.ToString(dr["UHPhone"]);
txtUCNumber.Text = Convert.ToString(dr["UCPhone"]); txtUEmail.Text = Convert.ToString(dr["UEmail"]);
ddlUState.Text = Convert.ToString(dr["UState"]);
txtUCity.Text = Convert.ToString(dr["UCity"]);
txtUAddress.Text = Convert.ToString(dr["UAddress"]);
// txtUComName.Enabled = true; txtUDesignation.Enabled = true; txtWPhone.Enabled = true; txtFaxNumber.Enabled = true; txtBCity.Enabled = true; txtCAddress.Enabled = true;
// txtECName.Enabled = true; txtECMNumber.Enabled = true; txtRelationship.Enabled = true; txtRHPhone.Enabled = true; txtECMNumber.Enabled = true;
//ddlUState.Text = Convert.ToString(dr["UState"]);
DateTime GV_date = DateTime.Now;
txtUDob.Text = GV_date.ToShortDateString();
this.GView.Visible = false;
txtFName.Focus();
this.btnUpdate.Enabled = true; this.btnShow.Enabled = false; this.btnSave.Enabled = false; this.btnAdd.Enabled = false; this.btnCancel.Enabled = true; this.btnClear.Enabled = false;
}

}

}
// End of Assigning Value to the foreign key of the Business Info and Emrg Contact Info Tables
}
Arasappan 4-Aug-15 9:08am    
Firstly bind the values in a lable.. And then asssign the values to the lable values.to the place u need..BEfore that u go with Findcontrol to bind the values
Altaful Haq 5-Aug-15 0:19am    
for example (a bit coding hints please)
Arasappan 5-Aug-15 0:48am    
Please go with rowcommand to edit.. it may help.. keep somm searching on row command..It may help u
Altaful Haq 5-Aug-15 1:43am    
protected void OnRowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GView, "Select$" + e.Row.RowIndex);
e.Row.ToolTip = "Click to select this row.";
}
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
foreach (GridViewRow row in GView.Rows)
{
if (row.RowIndex == GView.SelectedIndex)
{
row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
row.ToolTip = string.Empty;

txtUID.Text = GView.Rows[0].Cells[0].Text;
}
else
{
row.BackColor = ColorTranslator.FromHtml("#FFFFFF");
row.ToolTip = "Click to select this row.";
}
}
}
can you help me with it to send specific row data into webform.

Follow below simple steps:



  • Drag and drop a GridView from toolbox on Webpage where you wants to show the gridview data


  • Add a Button On which click event the GridView displays data


  • Create a connection like



SqLConnection con=new SqlConnection(ConfigurationManager.ConnectedStrings["NameOfYourConnectionString"].ToString());//for connection


  • Now goto the click event of button and code as below:

con.open();
SqlCommand cmd=new SqlCommand("select * from YourTableName",con);
SqlDataReader dr=cmd.ExecuteReader();
if(dr.Read())
{
   SqlDataAdapter da=new SqlDataAdapter(cmd);
   DataSet ds=new DataSet();
   da.Fill(ds);
   GridView.DataSource=ds;
   GridView.DataBind();
   con.close();
}
else
{
 //code for error
}
 
Share this answer
 
You are doing wrong.

Refer - GridView.RowEditing Event[^].
C#
protected void TaskGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
    //Set the edit index.
    TaskGridView.EditIndex = e.NewEditIndex;
    //Bind data to the GridView control.
    BindData();
}
 
Share this answer
 
v2
Comments
Altaful Haq 5-Aug-15 0:45am    
@Tadit Dash it does not working dear...
This is just a general idea, not a solution. You need to dig more on this.
Altaful Haq 5-Aug-15 2:53am    
thanks
Altaful Haq 5-Aug-15 2:51am    
I figure out something else but quite Useful so for
www.youtube.com/watch?v=VtRp8RhNk2U
Good. Try to learn about Editing, which you need.

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