Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Im unable to get the quantity as per selection in SelectedIndexChanged

What I have tried:

C#
protected void gvAgent_SelectedIndexChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow row in gvAgent.Rows)
        {
            if (row.RowIndex == gvAgent.SelectedIndex)
            {
                row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
                row.ToolTip = string.Empty;
            }
            else
            {
                row.BackColor = ColorTranslator.FromHtml("#FFFFFF");
                row.ToolTip = "Click to select this row.";
            }
        }
  GridViewRow gvrow = gvAgent.SelectedRow;

        string Quantity = "";
        int SpareID = Convert.ToInt32(this.gvAgent.SelectedDataKey.Value.ToString());

        lblCustomerID.Text = "";
        lblSpareID.Text = gvrow.Cells[0].Text;
        //lblSpareID.Text = SpareID.ToString();
        DataTable dtData = objSpare.GetSpareValues((Convert.ToInt32(SpareID.ToString())), Quantity);
        txtPartDetails.Text = gvrow.Cells[3].Text;
        txtQuantity.Text = gvrow.Cells[6].Text;
        txtPrice.Text = gvrow.Cells[8].Text;

        txtPartDetails2.Text = gvrow.Cells[3].Text;
        txtQuantity2.Text = gvrow.Cells[6].Text;
        txtPrice2.Text = gvrow.Cells[8].Text;
Posted
Updated 18-Nov-17 2:41am
v2
Comments
Karthik_Mahalingam 18-Nov-17 8:42am    
post the markup of gridview
Shahbaz435 18-Nov-17 8:47am    

<asp:Label ID="lblMsg" runat="server" Text="" ForeColor="red">
   
<asp:GridView ID="gvAgent" runat="server" AutoGenerateColumns="False" DataKeyNames="SpareID" OnSelectedIndexChanged="gvAgent_SelectedIndexChanged" OnRowDataBound="gvAgent_RowDataBound" AllowPaging="True" CssClass="mGrid" Width="100%" EnablePersistedSelection="True">
<columns>
<asp:BoundField DataField="SpareID" HeaderText="Spare ID" />
<asp:BoundField HeaderText="Supplier Code" DataField="SupplierCode" />
<asp:BoundField HeaderText="Part Number" DataField="PartNumber" />
<asp:BoundField HeaderText="Part Name" DataField="PartName" />
<asp:BoundField HeaderText="Vehicle Name" DataField="VehicleName" />
<asp:BoundField HeaderText="Description" DataField="Description" />
<asp:BoundField HeaderText="Qty" DataField="Quantity" />
<asp:BoundField HeaderText="Cost Price" DataField="CostPrice" />
<asp:BoundField HeaderText="Actual Price" DataField="ActualPrice" />
<asp:BoundField HeaderText="X Part's" DataField="XPart" />
<asp:BoundField HeaderText="Date" DataField="Date" />
<%--<asp:TemplateField HeaderText="Edit">--%>
<%--<itemtemplate>
<asp:ImageButton ID="ImgEdit" runat="server" CausesValidation="False" CommandName="Edit" ToolTip="Click here to Edit " ImageAlign="Middle" ImageUrl="Images/edit.png" />


<asp:TemplateField HeaderText="Delete" Visible="false">
<itemtemplate>
<asp:ImageButton ID="ImgDelete" runat="server" CausesValidation="False" CommandName="Delete" ToolTip="Click here to Delete" ImageAlign="Middle" ImageUrl="Images/delet.png" />


<asp:TemplateField HeaderText="View">
<itemtemplate>
<asp:ImageButton ID="ImgView" runat="server" CausesValidation="False" ToolTip="Click here to View" ImageAlign="Middle" ImageUrl="Images/view.png" />


<asp:TemplateField HeaderText="Select">
<itemtemplate>
<asp:CheckBox ID="ChkSelect" runat="server" />

--%>

<PagerStyle CssClass="pgr" />
<AlternatingRowStyle CssClass="alt" />

 
Karthik_Mahalingam 18-Nov-17 8:50am    
is the value displaying on the screen ?
Shahbaz435 18-Nov-17 8:51am    
yes but i want selected data in my text boxes and also showing me the quantity as per my selection
Karthik_Mahalingam 18-Nov-17 8:59am    
did you try debugging the selected row object

1 solution

html page source



<title>




<asp:GridView ID="grid1" runat="server" OnRowDataBound="grid1_RowDataBound" OnSelectedIndexChanged="grid1_SelectedIndexChanged">






NAME : <asp:TextBox ID="txtname" runat="server" Width="300px">


CODE : <asp:TextBox ID="txtcode" runat="server" Width="300px">






//code page

public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("NAME");
dt.Columns.Add("CODE");

DataRow dr1 = dt.NewRow();

dr1[0] = "hi";
dr1[1] = "coo1";
dt.Rows.Add(dr1);

DataRow dr = dt.NewRow();

dr[0] = "hello";
dr[1] = "coo2";
dt.Rows.Add(dr);

grid1.DataSource = dt;
grid1.DataBind();

}
}
protected override void Render(HtmlTextWriter writer)
{
const string onMouseOverStyle = "this.className='GridViewMouseOver';";
//const string onMouseOutStyle = "this.className='{0}';";

foreach (GridViewRow gvr in grid1.Rows)
{
gvr.Attributes["onmouseover"] = onMouseOverStyle;
//gvr.Attributes["onmouseout"] = String.Format(
// onMouseOutStyle,
gvr.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(grid1,String.Concat("Select$", gvr.RowIndex),
true);
}

base.Render(writer);
}
protected void grid1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = grid1.SelectedRow;
txtname.Text = row.Cells[0].Text;
txtcode.Text = row.Cells[1].Text;
}

protected void grid1_RowDataBound(object sender, GridViewRowEventArgs e)
{

}
}
 
Share this answer
 
Comments
Laxmidhar tatwa technologies 20-Nov-17 23:36pm    
this is working fine whatever else ?
Laxmidhar tatwa technologies 20-Nov-17 23:39pm    
this is working well
Laxmidhar tatwa technologies 21-Nov-17 1:41am    
the above code working well for simple two fields and adding mouse listner

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