Click here to Skip to main content
14,976,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,
I have a problem with gridview as i'm new to web application and expecting a solution to the below code. It goes like this,

C#
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridView row = GridView1.SelectedRow;
        txtLastName.Text = GridView1.SelectedRow.RowIndex[0].ToString();
        txtFirstName.Text = GridView1.SelectedRow.RowIndex[1].ToString();
        txtEmailAddress.Text = GridView1.SelectedRow.RowIndex[2].ToString();
        txtPhoneNumber.Text = GridView1.SelectedRow.RowIndex[3].ToString();
        txtDOB.Text = GridView1.SelectedRow.RowIndex[4].ToString();
    }


how shall i proceed. I'm getting error it reads like this Cannot apply indexing with [] to an expression of type 'int'

Thanks in advance
Posted
Comments
Ankur\m/ 3-Aug-12 6:27am
   
Which line? Also did you search Google for the error message?
Manjunatha Shankar 3-Aug-12 6:42am
   
Hi Ankur,
Its first line GridView row = GridView1.SelectedRow; and i'm getting error and goes like this - Cannot implicitly convert type 'System.Web.UI.WebControls.GridViewRow' to 'System.Web.UI.WebControls.GridView'

any help please
Thanks in advance

I'm going to provide a meta-solution here. Do you actually understand what the code you pasted is trying to do? Both compiler errors that you complain about are quite clear and shouldn't require you to post on a help website to resolve.

Learn to use the MSDN documentation for the Framework. It will tell you that GridView.SelectedRow returns something of type GridViewRow, so you need to declare GridViewRow row = GridView1.SelectedRow;. And it will tell you that to get a cell out of a GridViewRow you need to use the Cells property: txtLastName.Text = row.Cells[0].ToString().

When you get a compiler error, Google will help you find the cause. But since you didn't seem able to resolve some simple and clear ones, I think you perhaps need to introduce yourself to C# and .Net more slowly and through a structured course.

Why do you declare row and then not use it? Why look up SelectedRow every line when you just saved it into a variable?
   
Comments
Manjunatha Shankar 3-Aug-12 13:12pm
   
hi BobJanova,
thanks for ur reply, also for ur advice. Yeah, i have to admit that i'm new to .NET and C# and i will improve to my best.
thanks again.:)
First of all use GridViewSelectEventArgs in stead of EventArgs.
Second: Object mismatch between GridView and GridViewRow:
GridView row = GridView1.SelectedRow;

then:
C#
protected void GridView1_SelectedIndexChanged(object sender, GridViewSelectEventArgs e)
{
GridViewRow row = GridView1.Rows[e.NewSelectedIndex];

// or you can do
C#
GridView1.SelectedRow = e.NewSelectedIndex;

and then not forget to set the selectedIndex to -1 if not needed anymore after this call
   
try some thing like this.

C#
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridView row = GridView1.SelectedRow;
        txtLastName.Text =  GridView1.SelectedRow.Cells[0].Text.ToString();
        txtFirstName.Text =  GridView1.SelectedRow.Cells[1].Text.ToString();
        txtEmailAddress.Text =  GridView1.SelectedRow.Cells[2].Text.ToString();
        txtPhoneNumber.Text =  GridView1.SelectedRow.Cells[3].Text.ToString();
        txtDOB.Text =  GridView1.SelectedRow.Cells[4].Text.ToString();
    }
   

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