will give you the current row.. You are Trying to comapare the currentRow Object directly with a int value which is wrong. You need to compare the index of the Row and not the Row Object directly.
Instead of
if (gdvCustomerDetailes.SelectedRow != -1)
{
// Your code here
}
//// Try This one
if(e.NewEditIndex != -1)
{
// Your code here
}
i tried above code iam getting Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
the place in code here
if (e.NewEditIndex != -1)
{
if (gdvCustomerDetailes.SelectedRow.Cells[0]!=null)
//this place Error:Object reference not set to an instance of an object.
Hello sushanth , thanks to reply to me with answers. what ever you suggest code working currectly and not showing any errors also
but small problem the value not comming to the txtbox:FirstName
when iam in debugging step in to the place:txtFirstName.Text = dvCustomerDetailes.Rows[e.NewEditIndex].Cells[0].Text;""value is empty null so the txtFirst name receiving null value so the cell value not comming into txtbox.
when iam direclty putting row nomberEx;1 in place of Rows[1] :-
ramesh.radam
Member
56 Points
85 Posts
Error 1 Operator '!=' cannot be applied to operands of type 'System.Web.UI.WebControls.GridViewRo...
Feb 22, 2012 03:57 PM|LINK
I want assign selected grid cell value to the respected textbox in this case i written only one txtbox that is txtFistName
my problem is when i grid edit button it will not assigning the value to the txtbox
could you please help me some body :Here is cs code below
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; public partial class Default8 : System.Web.UI.Page { public DataSet EmployeeDetailes { get { if (ViewState["EmployeeDetailes"] == null) { DataSet ds = new DataSet(); DataTable dt = new DataTable(); dt.Columns.Add("ID", Type.GetType("System.Int32")); dt.Columns.Add("First Name", Type.GetType("System.String")); dt.Columns.Add("Last Name", Type.GetType("System.String")); dt.Columns.Add("Gender", Type.GetType("System.String")); dt.Columns.Add("Salary", Type.GetType("System.Int32")); dt.Columns.Add("Contact Number", Type.GetType("System.Int64")); dt.Rows.Add(1, "Ramesh", "Radam", "Mail", 18000, 9030282001); dt.Rows.Add(1, "Satya", "Radam", "Mail", 18000, 9030282001); ds.Tables.Add(dt); } return (DataSet)ViewState["EmployeeDetailes"]; } set { ViewState["EmployeeDetailes"] = value; } } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindData(); } } private void BindData() { DataSet ds = new DataSet(); DataTable dt = new DataTable(); dt.Columns.Add("ID", Type.GetType("System.Int32")); dt.Columns.Add("FirstName", Type.GetType("System.String")); dt.Columns.Add("LastName", Type.GetType("System.String")); dt.Columns.Add("Gender", Type.GetType("System.String")); dt.Columns.Add("Salary", Type.GetType("System.Int32")); dt.Columns.Add("ContactNO", Type.GetType("System.Int64")); dt.Rows.Add(1, "Ramesh", "Radam", "Mail", 18000, 9030282001); dt.Rows.Add(1, "Satya", "Radam", "Mail", 18000, 9030282001); ds.Tables.Add(dt); gdvCustomerDetailes.DataSource = ds; gdvCustomerDetailes.DataBind(); } protected void gdvCustomerDetailes_RowEditing(object sender, GridViewEditEventArgs e) { gdvCustomerDetailes.EditIndex = e.NewEditIndex; BindData(); if (gdvCustomerDetailes.SelectedRow != -1)//*here iam gettin this error:Error 1 Operator '!=' cannot be applied to operands of type 'System.Web.UI.WebControls.GridViewRow' and 'int'*// { if (gdvCustomerDetailes.SelectedRow.Cells[0] != null) { txtFirstName.Text = gdvCustomerDetailes.SelectedRow.Cells[0].Text; } } } protected void gdvCustomerDetailes_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { gdvCustomerDetailes.EditIndex = -1; BindData(); txtFirstName.Text = ""; } }<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default8.aspx.cs" Inherits="Default8" EnableViewState="false" %> <!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> <table width="890px"> <tr> <td> <asp:Panel ID="pnlCustomerDetailes" runat="server"> <table width="890px"> <tr> <td style="background-color: Blue; width: 1px"> <asp:Image ID="Image1" Height="1px" ImageUrl="~/ramesh.gif" runat="server" /> </td> <td> <table width="890px"> <tr style="background-color: Blue"> <td colspan="4"> <asp:Label ID="Label1" runat="server" Text="CustomerDetailes" ForeColor="White"></asp:Label> </td> </tr> <tr> <td> <asp:Label ID="lblFirstName" runat="server" Text="FirstName"></asp:Label> </td> <td> <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox> </td> <td> <asp:Label ID="lblLastName" runat="server" Text="LastName"></asp:Label> </td> <td> <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox> </td> </tr> <tr> <td> <asp:Label ID="lblGender" runat="server" Text="Gender"></asp:Label> </td> <td> <asp:RadioButtonList ID="RadioButtonList1" RepeatDirection="Horizontal" runat="server"> <asp:ListItem Text="Male" Value="1"></asp:ListItem> <asp:ListItem Text="FeMale" Value="2"></asp:ListItem> </asp:RadioButtonList> </td> <td> </td> <td> </td> </tr> <tr> <td> <asp:Label ID="lblSalary" runat="server" Text="Salary"></asp:Label> </td> <td> <asp:TextBox ID="txtSalary" runat="server"></asp:TextBox> </td> <td> </td> <td> </td> </tr> <tr> <td> <asp:Label ID="lblContactNO" runat="server" Text="ContactNO"></asp:Label> </td> <td> <asp:TextBox ID="txtContactNo" runat="server"></asp:TextBox> </td> <td> </td> <td> </td> </tr> </table> </td> <td style="background-color: Blue; width: 1px"> <asp:Image ID="Image2" Height="1px" ImageUrl="~/ramesh.gif" runat="server" /> </td> </tr> <tr style="background-color: Blue"> <td colspan="3" style="height: 1px"> </td> </tr> </table> </asp:Panel> </td> </tr> <tr> <td style="height: 5px"> </td> </tr> <tr> <td> <asp:GridView ID="gdvCustomerDetailes" AutoGenerateColumns="False" runat="server" Font-Size="14px" Font-Names="Arial,Helvertica,scans-seif" HeaderStyle-BackColor="Blue" HeaderStyle-ForeColor="White" Width="890px" onrowediting="gdvCustomerDetailes_RowEditing" onrowcancelingedit="gdvCustomerDetailes_RowCancelingEdit" > <Columns> <asp:BoundField DataField="ID" HeaderText="CustomerID" ItemStyle-HorizontalAlign="Center" /> <asp:BoundField DataField="FirstName" HeaderText="FirstName" ItemStyle-HorizontalAlign="Center" /> <asp:BoundField DataField="LastName" HeaderText="LastName" ItemStyle-HorizontalAlign="Center" /> <asp:BoundField DataField="Gender" HeaderText="Gender" ItemStyle-HorizontalAlign="Center" /> <asp:BoundField DataField="Salary" HeaderText="Salary" ItemStyle-HorizontalAlign="Center" /> <asp:BoundField DataField="ContactNO" HeaderText="ContactNO" ItemStyle-HorizontalAlign="Center" /> <asp:CommandField ShowEditButton="true" /> </Columns> <HeaderStyle BackColor="Blue" ForeColor="White"></HeaderStyle> </asp:GridView> </td> </tr> </table> </div> </form> </body> </html>sushanth009
Contributor
6243 Points
1168 Posts
Re: Error 1 Operator '!=' cannot be applied to operands of type 'System.Web.UI.WebControls.GridVi...
Feb 22, 2012 04:30 PM|LINK
will give you the current row.. You are Trying to comapare the currentRow Object directly with a int value which is wrong. You need to compare the index of the Row and not the Row Object directly.
Instead of
if (gdvCustomerDetailes.SelectedRow != -1) { // Your code here } //// Try This one if(e.NewEditIndex != -1) { // Your code here }KathyW
Participant
1666 Points
303 Posts
Re: Error 1 Operator '!=' cannot be applied to operands of type 'System.Web.UI.WebControls.GridVi...
Feb 22, 2012 04:34 PM|LINK
A GridView.SelectedRow is not an integer. It's a row.
Are you trying to test SelectedIndex, instead? That's -1 if no row is selected, the selected row index otherwise.
ramesh.radam
Member
56 Points
85 Posts
Re: Error 1 Operator '!=' cannot be applied to operands of type 'System.Web.UI.WebControls.GridVi...
Feb 23, 2012 03:25 AM|LINK
Hi
Hello,sushanth
thanks for reply to me
i tried above code iam getting Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
the place in code here
if (e.NewEditIndex != -1) { if (gdvCustomerDetailes.SelectedRow.Cells[0]!=null) //this place Error:Object reference not set to an instance of an object.{ txtFirstName.Text = gdvCustomerDetailes.SelectedRow.Cells[0].Text; }if (e.NewEditIndex != -1) { txtFirstName.Text = gdvCustomerDetailes.SelectedRow.Cells[0].Text; }ramesh.radam
Member
56 Points
85 Posts
Re: Error 1 Operator '!=' cannot be applied to operands of type 'System.Web.UI.WebControls.GridVi...
Feb 23, 2012 03:32 AM|LINK
Hi
Hello kathyw
Thanks for replying to me
Could you please suggest me the code,where i need to change the code and how
KathyW
Participant
1666 Points
303 Posts
Re: Error 1 Operator '!=' cannot be applied to operands of type 'System.Web.UI.WebControls.GridVi...
Feb 23, 2012 03:54 AM|LINK
Simply replace your existing code with my replacement, SelectedIndex instead of SelectedRow, in the line that is throwing the error.
sushanth009
Contributor
6243 Points
1168 Posts
Re: Error 1 Operator '!=' cannot be applied to operands of type 'System.Web.UI.WebControls.GridVi...
Feb 23, 2012 03:33 PM|LINK
Replace selectedRow in your code with this and see if you see the same error.
///Instead of this .. if (e.NewEditIndex != -1) { if (gdvCustomerDetailes.SelectedRow.Cells[0]!=null) { txtFirstName.Text = dvCustomerDetailes.SelectedRow.Cells[0].Text; } if (e.NewEditIndex != -1) { txtFirstName.Text = gdvCustomerDetailes.SelectedRow.Cells[0].Text; } /////Try This if (e.NewEditIndex != -1) { if (gdvCustomerDetailes.Rows[e.NewEditIndex].Cells[0]!=null) { txtFirstName.Text = dvCustomerDetailes.Rows[e.NewEditIndex].Cells[0].Text; } if (e.NewEditIndex != -1) { txtFirstName.Text = gdvCustomerDetailes.Rows[e.NewEditIndex].Cells[0].Text; }This should take care of the error for you.
ramesh.radam
Member
56 Points
85 Posts
Re: Error 1 Operator '!=' cannot be applied to operands of type 'System.Web.UI.WebControls.GridVi...
Feb 24, 2012 04:44 AM|LINK
Hi
Hello sushanth , thanks to reply to me with answers. what ever you suggest code working currectly and not showing any errors also
but small problem the value not comming to the txtbox:FirstName
when iam in debugging step in to the place:txtFirstName.Text = dvCustomerDetailes.Rows[e.NewEditIndex].Cells[0].Text;""value is empty null so the txtFirst name receiving null value so the cell value not comming into txtbox.
when iam direclty putting row nomberEx;1 in place of Rows[1] :-
txtFirstName.Text = gdvCustomerDetailes.Rows[1].Cells[1].Text;
txtbox:txtFirstName receiving value of row:
sushanth009
Contributor
6243 Points
1168 Posts
Re: Error 1 Operator '!=' cannot be applied to operands of type 'System.Web.UI.WebControls.GridVi...
Feb 24, 2012 03:02 PM|LINK
What do you have in the first column
I think you have the FirstName in Cells[1]. .. What does your .aspx side of the Gridview Look like..
ramesh.radam
Member
56 Points
85 Posts
Re: Error 1 Operator '!=' cannot be applied to operands of type 'System.Web.UI.WebControls.GridVi...
Feb 24, 2012 08:10 PM|LINK
Hai
Hello sushanth
Cell[0]represents:CoustomerID that is 2 in case of row:-
dt.Rows.Add(1, "Ra", "hari", "Female", 18000, 9030282001); dt.Rows.Add(2, "Satya", "Radam", "Mail", 10, 9030282001); dt.Rows.Add(3, "xx", "badam", "Femail", 18000, 9030282001);these data in grid when iam click on edit button in First row asper code written the value:" ra" must me store in txtbox:txtFirstNamewhen iam giving row index directly set:Rows[1] :at place
txtFirstName.Text = gdvCustomerDetailes.Rows[1].Cells[0].Text;public partial class Default8 : System.Web.UI.Page { public DataSet EmployeeDetailes { get { if (ViewState["EmployeeDetailes"] == null) { DataSet ds = new DataSet(); DataTable dt = new DataTable(); dt.Columns.Add("ID", Type.GetType("System.Int32")); dt.Columns.Add("First Name", Type.GetType("System.String")); dt.Columns.Add("Last Name", Type.GetType("System.String")); dt.Columns.Add("Gender", Type.GetType("System.String")); dt.Columns.Add("Salary", Type.GetType("System.Int32")); dt.Columns.Add("Contact Number", Type.GetType("System.Int64")); dt.Rows.Add(1, "Ramesh", "Radam", "Mail", 18000, 9030282001); dt.Rows.Add(1, "Satya", "Radam", "Mail", 18000, 9030282001); ds.Tables.Add(dt); } return (DataSet)ViewState["EmployeeDetailes"]; } set { ViewState["EmployeeDetailes"] = value; } } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindData(); } } private void BindData() { DataSet ds = new DataSet(); DataTable dt = new DataTable(); dt.Columns.Add("ID", Type.GetType("System.Int32")); dt.Columns.Add("FirstName", Type.GetType("System.String")); dt.Columns.Add("LastName", Type.GetType("System.String")); dt.Columns.Add("Gender", Type.GetType("System.String")); dt.Columns.Add("Salary", Type.GetType("System.Int32")); dt.Columns.Add("ContactNO", Type.GetType("System.Int64")); dt.Rows.Add(1, "Ra", "hari", "Female", 18000, 9030282001); dt.Rows.Add(2, "Satya", "Radam", "Mail", 10, 9030282001); dt.Rows.Add(3, "xx", "badam", "Femail", 18000, 9030282001); ds.Tables.Add(dt); gdvCustomerDetailes.DataSource = ds; gdvCustomerDetailes.DataBind(); } protected void gdvCustomerDetailes_RowEditing(object sender, GridViewEditEventArgs e) { gdvCustomerDetailes.EditIndex = e.NewEditIndex; BindData(); if (e.NewEditIndex != -1) { if (gdvCustomerDetailes.Rows[e.NewEditIndex].Cells[0] != null) { txtFirstName.Text = gdvCustomerDetailes.Rows[e.NewEditIndex].Cells[2].Text; } } } protected void gdvCustomerDetailes_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { gdvCustomerDetailes.EditIndex = -1; BindData(); txtFirstName.Text = ""; } }