This is a migrated thread and some comments may be shown as answers.

RadGrid Delete/Insert/Update question

5 Answers 127 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ignjat
Top achievements
Rank 1
Ignjat asked on 23 Aug 2011, 04:25 PM
Hello!

I've spent couple of hours trying to figure out how to delete, update or insert data into database via RadGrid. Could you point me out what I'm doing wrong. What I'd like to accomplish is to edit the data via EditForm, insert data via "Add New Record" Command and delete the data.

I've been going through Telerik's documentation, but I'm totally lost.

Codefront:
<telerik:RadGrid ID="gvKontakti" runat="server" AllowPaging="True" AllowSorting="True"
    AutoGenerateColumns="False"
    CellSpacing="0" GridLines="None"
    OnUpdateCommand="gvKontakti_UpdateCommand" Skin="Default"
    onDeletecommand="gvKontakti_DeleteCommand"
    oninsertcommand="gvKontakti_InsertCommand">
    <MasterTableView DataKeyNames="idKontakt" CommandItemDisplay="TopAndBottom">
        <Columns>
            <telerik:GridEditCommandColumn UniqueName="EditCommandColumn">
            </telerik:GridEditCommandColumn>
            <telerik:GridBoundColumn UniqueName="Kontakt" DataField="Naziv" HeaderText="Kontakt">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="Funkcija" HeaderText="Funkcija" DataField="Funkcija">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="Tel1" HeaderText="Telefon 1" DataField="Tel1">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="Tel2" HeaderText="Telefon 2" DataField="Tel2">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="Mob1" HeaderText="Mobitel 1" DataField="Mob1">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="Mob2" HeaderText="Mobitel 2" DataField="Mob2">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="Email1" HeaderText="E-mail 1" DataField="E-mail1">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="Email2" HeaderText="E-mail 2" DataField="E-mail2">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="Fax" HeaderText="Fax" DataField="Fax">
            </telerik:GridBoundColumn>
            <telerik:GridButtonColumn ConfirmText="Obrisati ovog korisnika?" ConfirmDialogType="RadWindow"
                    ConfirmTitle="Brisanje" ButtonType="ImageButton" CommandName="Delete" />
        </Columns>
        <EditFormSettings EditFormType="Template">
            <EditColumn UniqueName="EditColumn">
            </EditColumn>
            <FormTemplate>
                <table>
                    <tr>
                        <td>
                            Ime:
                        </td>
                        <td>
                            <asp:TextBox ID="txtIme" runat="server" Text='<%# Bind("Ime") %>'></asp:TextBox>
                        </td>
                        <td>
                            Prezime:
                        </td>
                        <td>
                            <asp:TextBox ID="txtPrezime" runat="server" Text='<%# Bind("Prezime") %>'></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Funkcija:
                        </td>
                        <td colspan="3">
                            <asp:TextBox ID="txtFunkcija" runat="server" Width="430px" Text='<%# Bind("Funkcija") %>'></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Telefon 1:
                        </td>
                        <td>
                            <asp:TextBox ID="txtTel1" runat="server" Text='<%# Bind("Tel1") %>'></asp:TextBox>
                        </td>
                        <td>
                            Telefon 2:
                        </td>
                        <td>
                            <asp:TextBox ID="txtTel2" runat="server" Text='<%# Bind("Tel2") %>'></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Mobitel 1:
                        </td>
                        <td>
                            <asp:TextBox ID="txtMob1" runat="server" Text='<%# Bind("Mob1") %>'></asp:TextBox>
                        </td>
                        <td>
                            Mobitel 2:
                        </td>
                        <td>
                            <asp:TextBox ID="txtMob2" runat="server" Text='<%# Bind("Mob2") %>'></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Fax:
                        </td>
                        <td>
                            <asp:TextBox ID="txtFax" runat="server" Text='<%# Bind("Fax") %>'></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            E-mail 1:
                        </td>
                        <td>
                            <asp:TextBox ID="txtEmail1" runat="server" Text='<%# Bind("Email1") %>'></asp:TextBox>
                        </td>
                        <td>
                            E-mail 2:
                        </td>
                        <td>
                            <asp:TextBox ID="txtEmail2" runat="server" Text='<%# Bind("Email2") %>'></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Adresa 1:
                        </td>
                        <td colspan="3">
                            <asp:TextBox ID="txtAdresa1" runat="server" Width="430px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Adresa 2:
                        </td>
                        <td colspan="3">
                            <asp:TextBox ID="txtAdresa2" runat="server" Width="430px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Adresa 3:
                        </td>
                        <td colspan="3">
                            <asp:TextBox ID="txtAdresa3" runat="server" Width="430px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Grad:
                        </td>
                        <td>
                            <asp:TextBox ID="txtGrad" runat="server"></asp:TextBox>
                        </td>
                        <td>
                            Poštanski broj:
                        </td>
                        <td>
                            <asp:TextBox ID="txtPBR" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Država:
                        </td>
                        <td>
                            <asp:TextBox ID="txtDrzava" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Bilješke:
                        </td>
                        <td colspan="3">
                            <asp:TextBox ID="txtBiljeske" runat="server" Width="455px" Height="200px" TextMode="MultiLine"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Aktivan korisnik:
                        </td>
                        <td colspan="3">
                            <asp:CheckBox ID="chkAktivan" runat="server" Checked='<%# Bind("Aktivan") %>' />
                        </td>
                    </tr>
                </table>
                <asp:Button ID="btnUpdate" CommandName="Update" Text="Update" runat="server" />
                <asp:Button ID="btnDelete" CommandName="Delete" Text="Delete" runat="server" />
            </FormTemplate>
        </EditFormSettings>
    </MasterTableView>
</telerik:RadGrid>

Codebehind:

protected void Page_Load(object sender, EventArgs e)
{
    var kontakti = BindContacts();
    gvKontakti.DataSource = kontakti;
    gvKontakti.DataBind();
}
 
public object BindContacts()
{
    TSEntities db = new TSEntities();
    var kontakti = from k in db.Kontakts
                   select new { k.idKontakt, Naziv = k.Ime + " " + k.Prezime, Funkcija = k.Funkcija, k.Ime, k.Prezime, k.Tel1, k.Tel2, k.Mob1, k.Mob2, k.Email1, k.Email2, k.Fax, k.Aktivan };
 
    return kontakti;
}
 
protected void gvKontakti_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
    TSEntities db = new TSEntities();
 
    if (e.CommandName == RadGrid.UpdateCommandName)
    {
        if (e.Item is GridEditFormItem)
        {
            GridEditFormItem item = (GridEditFormItem)e.Item;
            int idKontakt = Convert.ToInt32(item.GetDataKeyValue("IdKontakt"));
 
            if (idKontakt > 0)
            {
                Kontakt kontakt = new Kontakt();
                kontakt = db.Kontakts.SingleOrDefault(k => k.idKontakt == idKontakt);
 
                TextBox txtIme = (TextBox)item.FindControl("txtIme");
                TextBox txtPrezime = (TextBox)item.FindControl("txtPrezime");
 
                kontakt.Ime = txtIme.Text;
                kontakt.Prezime = txtPrezime.Text;
                db.SaveChanges();
 
                gvKontakti.Rebind();
            }
        }
    }
}
 
protected void gvKontakti_DeleteCommand(object sender, GridCommandEventArgs e)
{
    TSEntities db = new TSEntities();
    Kontakt kontakt = new Kontakt();
 
    int idKontakt = (int)((GridDataItem)e.Item).GetDataKeyValue("IdKontakt");
 
    kontakt = db.Kontakts.SingleOrDefault(k => k.idKontakt == idKontakt);
 
    if (kontakt != null)
    {
        db.Kontakts.DeleteObject(kontakt);
        try
        {
            db.SaveChanges();
        }
        catch (System.Exception)
        {
             
        }
    }
 
}
 
protected void gvKontakti_InsertCommand(object sender, GridCommandEventArgs e)
{
    TSEntities db = new TSEntities();
}


Any help would be appreciated.

5 Answers, 1 is accepted

Sort by
0
Accepted
Elliott
Top achievements
Rank 2
answered on 23 Aug 2011, 05:56 PM
first of all, the buttons you need to use are Edit and Delete
the Edit button, when clicked, will change to Update and Cancel
0
Accepted
Elliott
Top achievements
Rank 2
answered on 23 Aug 2011, 05:58 PM
secondly, I would populate your grid with a NeedDataSource event handler - let the grid decide when it needs refreshing
0
Accepted
Elliott
Top achievements
Rank 2
answered on 23 Aug 2011, 06:02 PM
thirdly, get rid of the EditFormTemplate and all those textboxes - set the EditMode to what your user prefers
I use UserControls but it's a bit more code
then grab the DataItem from the UpdateCommand data handler for your new values
0
Ignjat
Top achievements
Rank 1
answered on 23 Aug 2011, 07:01 PM
Thank you! Those were the suggestions I was looking for! ;)
0
Elliott
Top achievements
Rank 2
answered on 23 Aug 2011, 07:30 PM
пожалуйста

(you're welcome)
Tags
Grid
Asked by
Ignjat
Top achievements
Rank 1
Answers by
Elliott
Top achievements
Rank 2
Ignjat
Top achievements
Rank 1
Share this question
or