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

Update database from gridview

4 Answers 189 Views
Grid
This is a migrated thread and some comments may be shown as answers.
newguy
Top achievements
Rank 1
newguy asked on 02 May 2013, 09:54 AM
I have a GridView that gets its data from a Microsoft SQL database.

The Person table inside of my database has a few columns
ID, Name ,Surname, Age, Address and Status

The GridView only has 2 columns that get populated 'Name' and 'Status' from my Person table in the database.
The Status can either be 'Active' or 'Inactive'.

Then an additional update column was added to my grid view.
My gridView columns look like the following

<telerik:GridBoundColumn DataField="Name" HeaderText="First Name"
meta:resourcekey="GridBoundColumnResource1" UniqueName="fName" Resizable="False" />
 
<telerik:GridBoundColumn DataField="Status" HeaderText="Status"
meta:resourcekey="GridBoundColumnResource2" UniqueName="currentStatus" Resizable="False" />
 
<telerik:GridTemplateColumn UniqueName="TemplateUpdateColumn">
<ItemTemplate>
<asp:LinkButton ID="Update" runat="server" AutoPostBack="true" " Text="Update" ><img src="../icons/update.ico"/></asp:LinkButton>
</ItemTemplate>
</telerik:GridTemplateColumn>

Each row that gets added into my GridView it will also have an update button.

All that i need to happen is that when i click on the update button for that specific item in the GridView the corrasponding item in my database my be set from active to Inactive.

i am using C# asp.net and linq.

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 02 May 2013, 11:10 AM
hi,

Please try the following code snippet i tried.

C#:
protected void Update_Click(object sender, EventArgs e)
{
    LinkButton btn = (LinkButton)sender;
    GridDataItem ditem = (GridDataItem)btn.NamingContainer;
    if (ditem["currentStatus"].Text == "Active")
    {
        //Update in dataBase
        RadGrid1.Rebind();
    }
}

Thanks,
Princy.
0
newguy
Top achievements
Rank 1
answered on 02 May 2013, 11:49 AM
I'm sorry i got completely wrong, in my GridView i have the following columns 'Name' and 'Surname' the Status Column doesn't get shown.

How would i access the ID of a specific person now ?
I cannot use the Name+surname text values of the the items populates on the gridview.
ditem["currentStatus"].Text == "Active"

is there any other way to find out what the current ID for that specific record is in the gridView so i can update the database accordingly
0
newguy
Top achievements
Rank 1
answered on 02 May 2013, 12:09 PM
Cant something like this be done

protected void Grid1_ItemCreated(object sender, GridItemEventArgs e)
{
         if (e.Item is GridDataItem)
          {
                   LinkButton UpdateButton = (LinkButton)e.Item.FindControl("Update");
                   UpdateButton .Attributes["href"] = "#";
                   UpdateButton .Attributes["onclick"] =  ??????????
          }
}
0
Accepted
Princy
Top achievements
Rank 2
answered on 03 May 2013, 04:39 AM
Hi,

I am not sure about your requirement . I guess you can set the ID as the DatakeyName in RadGrid and access the DataKeyValue in Update button key as follows.

ASPX:
<MasterTableView CommandItemDisplay="Top" DataKeyNames="ID">

C#:
protected void Update_Click(object sender, EventArgs e)
{
    LinkButton btn = (LinkButton)sender;
    GridDataItem ditem = (GridDataItem)btn.NamingContainer;
    if (ditem["currentStatus"].Text == "Active")
    {
        string datakey = ditem.GetDataKeyValue("ID").ToString();
        //Update in dataBase
        RadGrid1.Rebind();
    }
}

Please elaborate your scenario if it doesn't help.

Thanks,
Princy.
Tags
Grid
Asked by
newguy
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
newguy
Top achievements
Rank 1
Share this question
or