0% found this document useful (0 votes)
79 views

Updations in Datagrid

The document contains code for managing a datagrid control in ASP.NET. It includes event handlers for deleting, editing, canceling and updating items in the datagrid. When an item is deleted, it removes the associated record from the database. When edited, it validates the quantity entered against inventory and updates the database or displays an error. It binds data to columns in the grid to display product details.

Uploaded by

api-3848319
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

Updations in Datagrid

The document contains code for managing a datagrid control in ASP.NET. It includes event handlers for deleting, editing, canceling and updating items in the datagrid. When an item is deleted, it removes the associated record from the database. When edited, it validates the quantity entered against inventory and updates the database or displays an error. It binds data to columns in the grid to display product details.

Uploaded by

api-3848319
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

Grid item value to be replaced means

Or Datalist item value

After databind

Foreach(Datagriditem or Datalistitem li in datagriedid or datalistid)


{
Label lb=(label)li.findcontrol(“label id”);
lb.text=lb.text.replace(“***”,”’”);
}

protected void DataGrid1_DeleteCommand(object source,


DataGridCommandEventArgs e)
{
string strdel = ((Label)e.Item.FindControl("lblproduct")).Text;
con.Open();
SqlCommand cmddel = new SqlCommand("delete from addcart where
productid='" + strdel + "'", con);
cmddel.ExecuteNonQuery();
con.Close();
fill();
}
protected void DataGrid1_CancelCommand(object source,
DataGridCommandEventArgs e)
{
fill();
DataGrid1.EditItemIndex = -1;
DataGrid1.DataBind();
}
protected void DataGrid1_EditCommand(object source,
DataGridCommandEventArgs e)
{
fill();
DataGrid1.EditItemIndex = e.Item.ItemIndex;
DataGrid1.DataBind();
}
protected void DataGrid1_UpdateCommand(object source,
DataGridCommandEventArgs e)
{
try
{
con.Open();
string proid =
((Label)e.Item.FindControl("lblproduct")).Text;
string productname =
((Label)e.Item.FindControl("lbproductname")).Text;
int quantity =
Convert.ToInt32(((TextBox)e.Item.FindControl("txtquantity")).Text);
float price =
float.Parse(((Label)e.Item.FindControl("lbunitprice")).Text);
SqlCommand cd = new SqlCommand("select quantity from
product where productid='" + proid + "'", con);
SqlDataReader dq = cd.ExecuteReader();
dq.Read();
int cnt = Convert.ToInt32(dq["quantity"].ToString());
dq.Close();
if (quantity > cnt)
{
Label1.Visible = true;
Label1.Text = "Available Quantity is:"+cnt;
}
else
{
Label1.Visible = false;
float subprice = quantity * price;
string queryup1 = "update addcart set quantity='" +
quantity.ToString() + "',price='" + subprice.ToString() + "' where
productid='" + proid.ToString() + "' and orderid='" +
Session["genid"].ToString() + "'";
SqlCommand cmd1 = new SqlCommand(queryup1, con);
cmd1.ExecuteNonQuery();

con.Close();
DataGrid1.EditItemIndex = -1;
fill();
}
}

protected void DataGrid1_DeleteCommand(object source,


DataGridCommandEventArgs e)
{
string strdel = ((Label)e.Item.FindControl("lblproduct")).Text;
con.Open();
SqlCommand cmddel = new SqlCommand("delete from addcart where
productid='" + strdel + "'", con);
cmddel.ExecuteNonQuery();
con.Close();
fill();
}
protected void DataGrid1_CancelCommand(object source,
DataGridCommandEventArgs e)
{
fill();
DataGrid1.EditItemIndex = -1;
DataGrid1.DataBind();
}
protected void DataGrid1_EditCommand(object source,
DataGridCommandEventArgs e)
{
fill();
DataGrid1.EditItemIndex = e.Item.ItemIndex;
DataGrid1.DataBind();
}
protected void DataGrid1_UpdateCommand(object source,
DataGridCommandEventArgs e)
{
try
{
con.Open();
string proid =
((Label)e.Item.FindControl("lblproduct")).Text;
string productname =
((Label)e.Item.FindControl("lbproductname")).Text;
int quantity =
Convert.ToInt32(((TextBox)e.Item.FindControl("txtquantity")).Text);
float price =
float.Parse(((Label)e.Item.FindControl("lbunitprice")).Text);
SqlCommand cd = new SqlCommand("select quantity from
product where productid='" + proid + "'", con);
SqlDataReader dq = cd.ExecuteReader();
dq.Read();
int cnt = Convert.ToInt32(dq["quantity"].ToString());
dq.Close();
if (quantity > cnt)
{
Label1.Visible = true;
Label1.Text = "Available Quantity is:"+cnt;
}
else
{
Label1.Visible = false;
float subprice = quantity * price;
string queryup1 = "update addcart set quantity='" +
quantity.ToString() + "',price='" + subprice.ToString() + "' where
productid='" + proid.ToString() + "' and orderid='" +
Session["genid"].ToString() + "'";
SqlCommand cmd1 = new SqlCommand(queryup1, con);
cmd1.ExecuteNonQuery();

con.Close();
DataGrid1.EditItemIndex = -1;
fill();
}
}
<asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False"
OnDeleteCommand="DataGrid1_DeleteCommand"
OnEditCommand="DataGrid1_EditCommand"
OnCancelCommand="DataGrid1_CancelCommand"
OnUpdateCommand="DataGrid1_UpdateCommand" BackColor="#E0E0E0" >

<AlternatingItemStyle
BackColor="#F3F9EF"></AlternatingItemStyle>

<HeaderStyle BackColor="#505050"
CssClass="whtext"></HeaderStyle>

<Columns>

<asp:TemplateColumn>

<ItemTemplate>

<asp:Label runat="server" ID="lblproduct"


Text='<%#DataBinder.Eval(Container.DataItem,"productid") %>'
Visible="false">

</asp:Label>

</ItemTemplate>

</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="SKU#">

<HeaderStyle Font-Bold="True" ></HeaderStyle>

<ItemTemplate>

<asp:Label Runat="server" ID="img" Font-


Bold="true"
Text='<%#DataBinder.Eval(Container.DataItem,"item")%>'></asp:Label>

</ItemTemplate>

</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="Product Name">

<HeaderStyle Font-Bold="True" ></HeaderStyle>

<ItemTemplate>

<asp:Label Runat=server ID="lbproductname"


Font-Bold=true Font-Size=Medium
Text='<%#DataBinder.Eval(Container.DataItem,"productname")%>'>

</asp:Label><br />

<small><i><b><asp:Label ID="lble" runat="server"


Text="ENGRAVING NAMES"></asp:Label></b></i></small><br />

<b><asp:Label ID="lbname1" Text="Name1:"


runat="server" ></asp:Label></b><asp:Label ID="lbl1"
runat="server"></asp:Label><br />

<b><asp:Label ID="lbname2" Text="Name2:"


runat="server" ></asp:Label></b><asp:Label ID="lbl2"
runat="server"></asp:Label><br />

<b><asp:Label ID="lbname3" Text="Name3:"


runat="server" ></asp:Label></b><asp:Label ID="lbl3"
runat="server"></asp:Label><br />

<b><asp:Label ID="lbname4" Text="Name4:"


runat="server" ></asp:Label></b><asp:Label ID="lbl4"
runat="server"></asp:Label>

</ItemTemplate>

</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="Quantity">

<HeaderStyle Font-Bold="True" ></HeaderStyle>

<ItemTemplate>
<asp:Label Runat=server ID="lblquantity"
Font-Bold=true Font-Size=Medium
Text='<%#DataBinder.Eval(Container.DataItem,"Quantity")%>'>

</asp:Label>

</ItemTemplate>

<EditItemTemplate>

<asp:TextBox runat=server Font-Bold=true


CssClass="text" Font-Size=Medium Width=50 ID="txtquantity"
Text='<%#DataBinder.Eval(Container.DataItem,"Quantity")%>'>

</asp:TextBox>

</EditItemTemplate>

</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="Unit Price">

<HeaderStyle Font-Bold="True" ></HeaderStyle>

<ItemTemplate>

<asp:Label Runat=server Font-Bold=true


Font-Size=Medium ID="lbunitprice" text='
<%#DataBinder.Eval(Container.DataItem,"unitprice")%>'>

</asp:Label>

</ItemTemplate>

</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="Price">

<HeaderStyle Font-Bold="True" ></HeaderStyle>

<ItemTemplate>

<asp:Label Runat=server ID="lbprice"


Font-Size=Medium Font-Bold=true Text='
<%#DataBinder.Eval(Container.DataItem,"price")%>'>

</asp:Label>

</ItemTemplate>

</asp:TemplateColumn>
<asp:EditCommandColumn UpdateText="Update"
HeaderText="&lt;b&gt;Edit&lt;/b&gt;"

CancelText="Cancel"
EditText="Edit"></asp:EditCommandColumn>

<asp:TemplateColumn>

<ItemTemplate>

<asp:LinkButton Text="Delete"
CssClass="text" Runat="server" ID="deleterow"
CommandName="Delete"></asp:LinkButton>

</ItemTemplate>

</asp:TemplateColumn>

</Columns>

</asp:datagrid>

You might also like