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

Retrieving primary key from Grid

2 Answers 115 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, 11:28 AM
Hello!

I need help figuring out how to pass the primary key value to the btnSave inside an EditForm. What I'd like to do is to get the primary key as a CommandArgument so I do some logic with it, but what I get is an error that the input string was not in a correct format with a following line in codebehind as a problem:

int value = Convert.ToInt32(e.CommandArgument);


Here is the codefront:

<telerik:RadGrid ID="gvKontakti" runat="server" AllowPaging="True" AllowSorting="True"
    CellSpacing="0" GridLines="None" DataKeyNames="idKontakt" AutoGenerateColumns="false"
    OnDeleteCommand="gvKontakti_DeleteCommand" OnEditCommand="gvKontakti_EditCommand"
    OnInsertCommand="gvKontakti_InsertCommand">
    <MasterTableView CommandItemDisplay="TopAndBottom">
        <Columns>
            <telerik:GridEditCommandColumn UniqueName="EditCommandColumn">
            </telerik:GridEditCommandColumn>
            <telerik:GridBoundColumn UniqueName="Naziv" HeaderText="Naziv" DataField="Naziv">
            </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>
        </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>
                </table>
                <asp:Button ID="btnSave" CommandName="Save" Text="Spremi" runat="server" />
                <asp:Button ID="btnDelete" CommandName="Delete" Text="Delete" runat="server" />
            </FormTemplate>
        </EditFormSettings>
    </MasterTableView>
protected void gvKontakti_EditCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            int idKontakt = 0;
            int value = Convert.ToInt32(e.CommandArgument);
 
            if (e.CommandName == "Spremi")
            {
                TSEntities db = new TSEntities();
                Kontakt kontakt = new Kontakt();
 
                if (idKontakt > 0)
                {
                    kontakt = db.Kontakts.SingleOrDefault(k => k.idKontakt == idKontakt);
 
                    TextBox txtIme = (TextBox)gvKontakti.FindControl("txtIme");
                    TextBox txtPrezime = (TextBox)gvKontakti.FindControl("txtPrezime");
                    TextBox txtFunkcija = (TextBox)gvKontakti.FindControl("txtFunkcija");
                    TextBox txtTel1 = (TextBox)gvKontakti.FindControl("txtTel1");
                    TextBox txtTel2 = (TextBox)gvKontakti.FindControl("txtTel2");
                    TextBox txtMob1 = (TextBox)gvKontakti.FindControl("txtMob1");
                    TextBox txtMob2 = (TextBox)gvKontakti.FindControl("txtMob2");
                    TextBox txtFax = (TextBox)gvKontakti.FindControl("txtFax");
                    TextBox txtEmail1 = (TextBox)gvKontakti.FindControl("txtEmail1");
                    TextBox txtEmail2 = (TextBox)gvKontakti.FindControl("txtEmail2");
                    TextBox txtAdresa1 = (TextBox)gvKontakti.FindControl("txtAdresa1");
                    TextBox txtAdresa2 = (TextBox)gvKontakti.FindControl("txtAdresa2");
                    TextBox txtAdresa3 = (TextBox)gvKontakti.FindControl("txtAdresa3");
                    TextBox txtGrad = (TextBox)gvKontakti.FindControl("txtGrad");
                    TextBox txtPBR = (TextBox)gvKontakti.FindControl("txtPBR");
                    TextBox txtDrzava = (TextBox)gvKontakti.FindControl("txtDrzava");
                    TextBox txtBiljeske = (TextBox)gvKontakti.FindControl("txtBiljeske");
                    CheckBox chkAktivan = (CheckBox)gvKontakti.FindControl("chkAktivan");
 
                    kontakt.Ime = txtIme.Text;
                    kontakt.Prezime = txtPrezime.Text;
                }
                else
                {
 
                }
                db.SaveChanges();
            
            }
        }


Any help would be appreciated.

2 Answers, 1 is accepted

Sort by
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 23 Aug 2011, 11:53 AM
Hello,

<MasterTableView DataKeyNames="ID">
ItemCommand()
{
  if(e.CommandName == "CommandName")
  {
      GridDataItem item = e.Item as GridDataItem;
     string strID = item.GetDataKeyValue("ID").ToString(); // get Your Primary key from here
   }
}

Let me know if any Concern.


Thanks,
Jayesh Goyani
0
Jayesh Goyani
Top achievements
Rank 2
answered on 23 Aug 2011, 11:56 AM
Hello,

<asp:Button ID="btnSave" CommandName="Spermi" Text="Spremi" CommandArgument='<%# Eval("ID") %>' runat="server" />


Int ID =  Convert.ToInt32(e.CommandArgument().ToString());


Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Ignjat
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Share this question
or