<div>
<asp:DataGrid id="dg" runat="server" BorderColor="#3366CC" BorderWidth="1px"
AutoGenerateColumns="False" HorizontalAlign="Center" dth="63%"
OnCancelCommand="dg_CancelCommand" OnEditCommand="dg_EditCommand"
OnUpdateCommand="dg_UpdateCommand" DataKeyField="companyid"
BackColor="White" BorderStyle="None" CellPadding="4" Height="170px">
<HeaderStyle HorizontalAlign="Center" BackColor="#003399" Font-Bold="True"
ForeColor="#CCCCFF" /> <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<SelectedItemStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left"
Mode="NumericPages" /><ItemStyle BackColor="White" ForeColor="#003399" />
<AlternatingItemStyle /><Columns><asp:BoundColumn DataField="companyid"
ItemStyle-HorizontalAlign="Center" HeaderText="Company Id" ReadOnly="True" >
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn><asp:TemplateColumn HeaderText="ComapnyName">
<ItemTemplate><%#Eval("CompanyName")%></ItemTemplate><EditItemTemplate>
<asp:TextBox id="txtcompanyname" runat="server" Columns="55"
text='<%# Eval("CompanyName") %>' /></EditItemTemplate>
</asp:TemplateColumn>
<asp:templatecolumn headertext="Company Address"
itemstyle-horizontalalign="Center"><itemtemplate>
<%# Eval("companyAddress") %></itemtemplate>
<edititemtemplate>
<asp:TextBox id="txtcompanyAddress" runat="server" Columns="55"
text='<%# Eval("companyAddress") %>' />
</edititemtemplate><ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:templatecolumn>
<asp:EditCommandColumn ButtonType="LinkButton" EditText="Edit"
CancelText="Cancel" UpdateText="Update" /></Columns></asp:DataGrid>
code behind file is that
protected void Page_Load(object sender, EventArgs e)
{if (!Page.IsPostBack)
{
bindData();
}
}
protected void dg_CancelCommand(Object source,DataGridCommandEventArgs e)
{
dg.EditItemIndex = -1;
bindData();
}
protected void dg_EditCommand(Object source,DataGridCommandEventArgs e)
{
dg.EditItemIndex = e.Item.ItemIndex;
bindData();
}
protected void dg_UpdateCommand(Object source,DataGridCommandEventArgs e)
{
SqlConnection dbConn = null;
SqlCommand dCmd = null;
String comp = null;
String strConnection = null;
String strSQL = null;
int rowsAffected;
String address = null;
try
{ comp = ((TextBox)(e.Item.FindControl("txtComapnyName"))).Text;
address = ((TextBox)(e.Item.FindControl("txtComapnyAddress"))).Text;
strConnection = ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString;
dbConn = new SqlConnection(strConnection);
dbConn.Open();
strSQL = "UPDATE companydetail SET companyname='" + comp + "'" +",companyaddress=" + address +
" WHERE companyID=" +dg.DataKeys[e.Item.ItemIndex].ToString();
dCmd = new SqlCommand(strSQL, dbConn);
rowsAffected = dCmd.ExecuteNonQuery();
dg.EditItemIndex = -1;
bindData();
}
finally
{
if (dbConn != null)
{
dbConn.Close();
}
}
}
private void bindData()
{
SqlConnection dbConn = null;
SqlDataAdapter da = null;
DataTable dTable = null;
String strConnection = null;
String strSQL = null;
try
{ strConnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
dbConn = new SqlConnection(strConnection);
dbConn.Open();
strSQL = "SELECT companyid, companyname,companyaddress from companydetail order by companyname";
da = new SqlDataAdapter(strSQL, dbConn);
dTable = new DataTable();
da.Fill(dTable);
dg.DataSource = dTable;
dg.DataKeyField = "companyid";
dg.DataBind();
}
finally
{
if (dbConn != null)
{
dbConn.Close();
}
}
}