-1

I have a GridView in which I'm redirecting to popup from bounded button click which has its row value from id.

But its throwing an exception:

index is out of range bigger than 127

I want to change type of index but its not working.

Here's my code:

protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
    int index = Convert.Toint16(e.CommandArgument);

    TextBox1.Text = GridView1.Rows[index].Cells[4].Text;
    TextBox2.Text = GridView1.Rows[index].Cells[5].Text;
    TextBox3.Text = GridView1.Rows[index].Cells[6].Text;
    TextBox4.Text = GridView1.Rows[index].Cells[7].Text;
    TextBox5.Text = GridView1.Rows[index].Cells[8].Text;
    TextBox6.Text = GridView1.Rows[index].Cells[9].Text;
}
  • Please format your question properly and improve the grammar because it's hard to understand what you want. Also, please read: https://stackoverflow.com/help/mcve – Maciej Jureczko Nov 08 '17 at 10:55
  • what i'm saying is that i want to change data type of index to double because id is much bigger than integer range and i use id of row to pop up its row information to new window but i got error in changing data type "cannot change strig or esle to int " textbox 1 is for id which is cell no 4 when ever i click id bigger then 127 i get erorrr on index out of range – fahd bhatti Nov 08 '17 at 11:07
  • Please clarify your original post instead of commenting. – Maciej Jureczko Nov 08 '17 at 11:11
  • Possible duplicate of [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – VDWWD Nov 08 '17 at 11:39

2 Answers2

0

Maybe something like this:

foreach (GridViewRow gvRow in gridview1.Rows)
{
    if ((int)gridview1.DataKeys[gvRow.DataItemIndex].Value == index)
    {
        gridview1.SelectedIndex = gvRow.DataItemIndex;
        break;
    }
}
Sea Charp
  • 288
  • 1
  • 6
0

Try this to find the index:

Option 1:

GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);

int index = row.RowIndex;

Option 2:

In your button in .aspx add:

CommandArgument='<%# Container.DataItemIndex %>'

And in .cs:

int index = e.CommandArgument;

Option 3:

Try it with ToInt32 instead of ToInt16:

int index = Convert.ToInt32(e.CommandArgument);