hi all.
ok i have been able to create an error message - if the user tries to delete a row which has not been selected first; but it still deletes from the gridview...

void DeleteRowButton_Click(Object sender, EventArgs e) 
{

LinkButton btn = sender as LinkButton;
GridViewRow row = btn.NamingContainer as GridViewRow; 
 

if (row == GridView1.SelectedRow) { 
GridView1.DeleteRow(GridView1.SelectedIndex);

// delete file from server

}

else

lblDeleteResult.Text = "Please select this file first, if you wish to delete it"; 
 

 

}

but the asp.net delete command- Delete="Delete From tbl....Where.......etc" deletes the file from the gridview regardless. How can one prevent the delete command from running unless the row is selected first?

p.s. the delete command is specified in the .aspx file

thanks

enable /disable the delete button in gridview based on the row selection.then this same code will works for u.let me know it works for u?

good idea!, does anyone have some sample code to allow only the delete button on the 'selected row' visible?

hi,

u can create delete button with in gridview itslef.check this example

<asp:GridView ID="gdview"  runat="server" ShowFooter="true" AutoGenerateColumns="False"  DataKeyNames="CategoryID"  OnRowCancelingEdit="gdview_RowCancelingEdit" OnRowDeleting="gdview_RowDeleting" OnRowEditing="gdview_RowEditing" OnRowUpdating="gdview_RowUpdating" Width="100%" AllowPaging="True" PageSize="5" OnPageIndexChanging="gdview_PageIndexChanging" OnRowDataBound="gdview_RowDataBound"  >
        <Columns>
            <asp:BoundField HeaderText="Category Name" DataField="CategoryName" SortExpression="CategoryName" >
                <ItemStyle Height="20px" Width="150px" />
            </asp:BoundField>
            
            <asp:CommandField ShowEditButton="True">
                <ItemStyle Width="100px" />
            </asp:CommandField>
            <asp:TemplateField>
            
          
          
            <ItemTemplate>
            <asp:LinkButton ID="lnkdel" runat="server" Text="Delete" CommandName="Delete"></asp:LinkButton>
            
            </ItemTemplate>
            <ItemStyle Width="100px" />
        
            </asp:TemplateField>
        </Columns>
 

 </asp:GridView>
  protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            bindgrid();
            total = 0;
           
        }

    }

    public void bindgrid()
    {
        SqlConnection conn = new SqlConnection("Data Source='localhost';Initial Catalog='Northwind';Integrated Security=SSPI;Persist Security Info=False ");
        SqlCommand cmd = new SqlCommand("select CategoryName,CategoryID from Categories ", conn);

        SqlDataAdapter da = new SqlDataAdapter("", conn);
        da.SelectCommand = new SqlCommand("select CategoryName,CategoryID from Categories", conn);
        DataSet ds = new DataSet();
        da.Fill(ds, "data");
        gdview.DataSource = ds.Tables[0].DefaultView;
        gdview.DataBind();

      
    }
 protected void gdview_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
       
        int catid = int.Parse(gdview.DataKeys[0].Value.ToString());
       SqlConnection conn = new SqlConnection("Data Source='localhost';Initial Catalog='Northwind';Integrated Security=SSPI;Persist Security Info=False ");
        SqlDataAdapter da = new SqlDataAdapter("", conn);
        conn.Open();
        da.DeleteCommand = new SqlCommand("delete from Categories where CategoryID="+catid, conn);
        da.DeleteCommand.ExecuteNonQuery();
        conn.Close();
        bindgrid();
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.