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
ProgramFOX108.9K
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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 695
1 OriginalGriff 518
2 Abhinav S 313
3 thatraja 245
4 CPallini 130
0 Sergey Alexandrovich Kryukov 8,372
1 OriginalGriff 4,830
2 Peter Leow 3,784
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107


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