Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
sir I am Select Gridview RowIndex But He Selected -1 Value Please Help Me


C#
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
Updated 21-Dec-12 1:25am
v2
Comments
[no name] 21-Dec-12 7:32am    
problem ???

If the SelectedIndex property is -1, there's no row selected. -1 is also the default value, which indicates that no row is selected.
 
Share this answer
 
Comments
manoj s sherje 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
Thomas Daniels 21-Dec-12 7:48am    
First, the user must select a row, and then SelectedIndex isn't -1.
HTML
<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.')" />


C#
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);  
              }   
            }    
        }
 
Share this answer
 
v2

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