ASP.NET GridView with Combobox ObjectDataSource EasyUpdate
Posted on Sunday, May 20, 2012
|
No Comments
BLL CLass For Data Insert
{
[DataObject]
public class Client
{
public string ID { get; set; }
public string UserName { get; set; }
public string Address { get; set; }
public string AdminUserID { get; set; }
public string AdminUserName { get; set; }
private Result obResult = new Result();
private DALS obDAL = new DALS();
[DataObjectMethod(DataObjectMethodType.Insert)]
public Result Save(Client obClients)
{
string query = @"INSERT INTO Client([id],[name],[address],[userID]) VALUES (" + obClients.ID + ",'" + obClients.UserName + "', '" + obClients.Address + "','" + obClients.AdminUserID + "')";
obResult = obDAL.Create(query);
return obResult;
}
[DataObjectMethod(DataObjectMethodType.Select)]
public List<Client> SelectAll()
{
List<Client> obCLientList = new List<Client>();
string query = "Select * from Client";
obResult = obDAL.Select(query);
DataTable dt = obResult.Data as DataTable;
for (int i = 0; i < dt.Rows.Count; i++)
{
Client obClient = new Client();
obClient.ID = dt.Rows[i]["id"].ToString();
obClient.UserName = dt.Rows[i]["name"].ToString();
obClient.Address = dt.Rows[i]["address"].ToString();
obClient.AdminUserID = dt.Rows[i]["userID"].ToString();
obCLientList.Add(obClient);
}
return obCLientList;
}
[DataObjectMethod(DataObjectMethodType.Update)]
public int Update(Client obClients)
{
string query = "UPDATE Client set name='" + obClients.UserName + "', address='" + obClients.Address + "',userID='" + obClients.AdminUserID + "' where id=" + obClients.ID;
obResult = obDAL.Update(query);
if (obResult.ResultState)
{
return 1;
}
return 0;
}
[DataObjectMethod(DataObjectMethodType.Delete)]
public int Delete(Client obClients)
{
string query = "DELETE FROM Client where id="+ obClients.ID;
obResult = obDAL.Update(query);
if (obResult.ResultState)
{
return 1;
}
return 0;
}
}
ASPX GRID CODE
AutoGenerateColumns="False" DataSourceID="ObjectDataSource2"
DataKeyNames="id" onrowdatabound="GridView1_RowDataBound"
onrowupdating="GridView1_RowUpdating">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" ReadOnly="true" />
<asp:BoundField DataField="UserName" HeaderText="UserName"
SortExpression="UserName" />
<asp:BoundField DataField="Address" HeaderText="Address"
SortExpression="Address" />
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="ObjectDataSource1" DataTextField="UserName" DataValueField="AdminUserID" SelectedValue='<%# Eval("AdminUserID") %>' >
</asp:DropDownList>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
OldValuesParameterFormatString="original_{0}" SelectMethod="SelectAll"
TypeName="SampleASP.Model.Users"></asp:ObjectDataSource>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server"
DataObjectTypeName="SampleASP.Model.Client" DeleteMethod="Delete"
OldValuesParameterFormatString="original_{0}" SelectMethod="SelectAll"
TypeName="SampleASP.Model.Client" UpdateMethod="Update"
onupdated="ObjectDataSource2_Updated"
onupdating="ObjectDataSource2_Updating">
<UpdateParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="AdminUserID" Type="String" />
</UpdateParameters>
</asp:ObjectDataSource>
CodeBehid File Code
{
DropDownList ddl = GridView1.Rows[e.RowIndex].FindControl("DropDownList1") as DropDownList;
e.NewValues["AdminUserID"] = ddl.SelectedValue.ToString();
}