Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
sir I am Select Gridview RowIndex But He Selected -1 Value Please Help Me
 

protected void ImgBtnDelete_Click(object sender, ImageClickEventArgs e)//ImageClickEventArgs
        {
            int row = Convert.ToInt32(GridView1.SelectedIndex.ToString()) ;
            DataTable Dt = (DataTable) ViewState ["CurrentTable"] ;
            DataRow DrCurrentRow = null;
            int RowIndex = Convert.ToInt32(row);    
            //row.RowIndex
            if (Dt.Rows.Count > 1) 
            {
              Dt.Rows.Remove(Dt.Rows[RowIndex]);
              DrCurrentRow = Dt.NewRow();
              ViewState["CurrentTable"] = Dt;
              GridView1.DataSource = Dt;
              GridView1.DataBind();
              for (int i = 0; i < GridView1.Rows.Count - 1; i++) 
              {
                  GridView1.Rows[i].Cells[0].Text = Convert.ToString(i + 1);  
              }    
 
            }  
  
        }
[Edit]Code block added[/Edit]
Posted 21-Dec-12 1:24am
Edited 21-Dec-12 1:25am
v2
Comments
Krunal Rohit at 21-Dec-12 7:32am
   
problem ???
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If the SelectedIndex property is -1, there's no row selected. -1 is also the default value, which indicates that no row is selected.
  Permalink  
Comments
manoj s sherje at 21-Dec-12 7:36am
   
but sir i am give this image button
<asp:ImageButton ID="ImgBtnDelete" runat="server"
ImageUrl="~/Images/delete.jpg" Height="20px" Width="20px"
ImageAlign="AbsMiddle" onclick="ImgBtnDelete_Click" OnClientClick="javascript:return confirm('Are you sure you want to permanently delete this item? This action cannot be reversed.')" />
 
and this image button i ma deleting grid view row how is possible
please help
ProgramFOX at 21-Dec-12 7:48am
   
First, the user must select a row, and then SelectedIndex isn't -1.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

<asp:ImageButton ID="ImgBtnDelete" runat="server"
                   ImageUrl="~/Images/delete.jpg" Height="20px" Width="20px" onclick="ImgBtnDelete_Click"
                   ImageAlign="AbsMiddle"  OnClientClick="javascript:return confirm('Are you sure you want to permanently delete this item?  This action cannot be reversed.')" />
 
protected void ImgBtnDelete_Click(object sender, ImageClickEventArgs e)//ImageClickEventArgs
        {
            ImageButton ibtn1 = sender as ImageButton;
            int rowIndex = Convert.ToInt32(ibtn1.Attributes["RowIndex"]);
            //int  Row = Convert.ToInt32( GridView1.DataKeys[GridView1.SelectedIndex]);    
           // int row = Convert.ToInt32(GridView1.SelectedIndex.ToString()) ;
            DataTable Dt = (DataTable) ViewState ["CurrentTable"] ;
            DataRow DrCurrentRow = null;
            int RowIndex = Convert.ToInt32(rowIndex);    
            //row.RowIndex
            if (Dt.Rows.Count > 1) 
            {
              Dt.Rows.Remove(Dt.Rows[RowIndex]);
              DrCurrentRow = Dt.NewRow();
              ViewState["CurrentTable"] = Dt;
              GridView1.DataSource = Dt;
              GridView1.DataBind();
              for (int i = 0; i < GridView1.Rows.Count - 1; i++) 
              {
                  GridView1.Rows[i].Cells[0].Text = Convert.ToString(i + 1);  
              }   
            }    
        }
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 mhegazy94 460
1 Sergey Alexandrovich Kryukov 450
2 Kornfeld Eliyahu Peter 305
3 Ravi Bhavnani 200
4 Shemeemsha RA 160
0 Sergey Alexandrovich Kryukov 6,890
1 OriginalGriff 6,761
2 CPallini 5,350
3 George Jonsson 3,609
4 Gihan Liyanage 2,797


Advertise | Privacy | Mobile
Web02 | 2.8.140922.1 | Last Updated 22 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100