Change buttons Edit and Delete row of datagrid

Last post 05-09-2008 8:03 AM by ecbruck. 7 replies.

Sort Posts:

  • Change buttons Edit and Delete row of datagrid

    04-28-2008, 10:50 AM
    • Loading...
    • Jelmer850i
    • Joined on 03-16-2005, 11:07 PM
    • Posts 73

    Right now i've got a datagrid with data.
    There is a Edit and a Delete command, but they placed in the width. But the width is to big. So i try to add other buttons. Or Delete and edit below each other.

    Exampel:

     Row 1     Row 2    Row 3    Edit   Delete

    But i like this more:

     Row 1     Row 2    Row 3    Edit
                                             Delete

    Is that possible ?
    And how can i make that.... right now i use in de aspx code this columns for it:

    <asp:EditCommandColumn
    And: <asp:ButtonColumn [delete]

    Is this possible? And how?
    Thanks!

     

  • Re: Change buttons Edit and Delete row of datagrid

    04-28-2008, 11:12 AM
    • Loading...
    • ecbruck
    • Joined on 12-30-2005, 2:39 PM
    • Des Moines, IA
    • Posts 5,743
    • Moderator
      TrustedFriends-MVPs

    Use a TemplateColumn instead and simply template the Buttons as you want. Something like this: 

    <asp:templatecolumn>
    	<itemtemplate>
    		<asp:button id="btnEdit" runat="server" commandname="Edit" text="Edit" /><br />
    		<asp:button id="btnDelete" runat="server" commandname="Delete" text="Delete" />
    	</itemtemplate>
    </asp:templatecolumn>
    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

    protected void Post_Answered(object sender, EventArgs e) { if (this.MarkAsAnswered != null) { this.MarkAsAnswered(this, EventArgs.Empty); } }
  • Re: Change buttons Edit and Delete row of datagrid

    04-28-2008, 12:54 PM
    • Loading...
    • Jelmer850i
    • Joined on 03-16-2005, 11:07 PM
    • Posts 73

    I will try!

  • Re: Change buttons Edit and Delete row of datagrid

    04-29-2008, 4:17 AM
    • Loading...
    • Jelmer850i
    • Joined on 03-16-2005, 11:07 PM
    • Posts 73

    it doesnt work quite well... because i use this code to delete something:

    protected void datagrid1_DeleteCommand1(object source, DataGridCommandEventArgs e)

    {

    try

    {

    int rowToDelete = getID(e);JumpTide.news _news = new JumpTide.news();

    _news.Delete(rowToDelete);

    Bind();

    }

    catch (Exception ex)

    {

    lblError.Visible =
    true;

    lblError.Text = ex.ToString();

    }

    }

    protected Int32 getID(DataGridCommandEventArgs e)

    {

    HiddenField hidden;

    hidden = (HiddenField)e.Item.FindControl("Id");

    return Convert.ToInt32(hidden.Value);

    }

     

    this code doesnt work if u use the other buttons. Although i use the onclick instance on it...

  • Re: Change buttons Edit and Delete row of datagrid

    04-29-2008, 7:59 AM
    Answer
    • Loading...
    • ecbruck
    • Joined on 12-30-2005, 2:39 PM
    • Des Moines, IA
    • Posts 5,743
    • Moderator
      TrustedFriends-MVPs

    This code should work for you as long as you make sure to notate the OnDeleteCommand handler within the DataGrid control. You shouldn't be using the OnClick handler.

    <asp:datagrid id="DataGrid1" runat="server" ondeletecommand="DataGrid1_DeleteCommand">

    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

    protected void Post_Answered(object sender, EventArgs e) { if (this.MarkAsAnswered != null) { this.MarkAsAnswered(this, EventArgs.Empty); } }
  • Re: Change buttons Edit and Delete row of datagrid

    05-09-2008, 6:52 AM
    • Loading...
    • Jelmer850i
    • Joined on 03-16-2005, 11:07 PM
    • Posts 73

    I already did that...

     

    <asp:DataGrid ID="datagrid1" runat="server" AutoGenerateColumns="False"

    OnDeleteCommand="datagrid1_DeleteCommand1"

    oneditcommand="datagrid1_EditCommand"

  • Re: Change buttons Edit and Delete row of datagrid

    05-09-2008, 6:59 AM
    • Loading...
    • Jelmer850i
    • Joined on 03-16-2005, 11:07 PM
    • Posts 73

    Ok it works already there was a form problem.
    But is it possible to put the column where the buttons are to invisable ?
    Normal visitors should not be possible to show that buttons.

  • Re: Change buttons Edit and Delete row of datagrid

    05-09-2008, 8:03 AM
    • Loading...
    • ecbruck
    • Joined on 12-30-2005, 2:39 PM
    • Des Moines, IA
    • Posts 5,743
    • Moderator
      TrustedFriends-MVPs

    Since the DataGrid has no DataBound event, you're best be is to do this in the Page.PreRnder event. The columns are referenced by index, so do something like this: 

    protected void Page_PreRender(object sender, EventArgs e)
    {
    	DataGrid1.Columns[0].Visible = User.IsInRole("Edit");
    }
    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

    protected void Post_Answered(object sender, EventArgs e) { if (this.MarkAsAnswered != null) { this.MarkAsAnswered(this, EventArgs.Empty); } }
Page 1 of 1 (8 items)