Hi suhaas121,
You have to use GridViewRow FindControl method. Refer the updated code.
HTML
<asp:GridView ID="Gridorder" OnRowDataBound="Gridorder_RowDataBound" CssClass="table table-responsive"
AutoGenerateColumns="False" DataKeyNames="Id" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="White" HorizontalAlign="Center" BackColor="#284775" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Select All">
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID" Visible="false">
<ItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%#Eval("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Part Number">
<ItemTemplate>
<asp:Label ID="lblpart" runat="server" Text='<%#Eval("Part_number") %>'></asp:Label>
<asp:TextBox ID="txtpart" runat="server" Text='<%#Eval("Part_number") %>' Visible="false" Enabled="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Nomenclature">
<ItemTemplate>
<asp:Label ID="lblnom" runat="server" Text='<%#Eval("Nomenclature") %>'></asp:Label>
<asp:TextBox ID="txtnom" runat="server" Text='<%#Eval("Nomenclature") %>' Visible="false" Enabled="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Vendor Code">
<ItemTemplate>
<asp:Label ID="lblven" runat="server" Text='<%#Eval("Vendor_Code") %>'></asp:Label>
<asp:TextBox ID="txtven" runat="server" Text='<%#Eval("Vendor_Code") %>' Visible="false" Enabled="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:Label ID="lblqu" runat="server" Text='<%#Eval("quantity") %>'></asp:Label>
<asp:TextBox ID="txtqu" runat="server" Text='<%#Eval("quantity") %>' Visible="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity Available">
<ItemTemplate>
<asp:Label ID="lblqua" runat="server" Text='<%#Eval("quantity_available") %>'></asp:Label>
<asp:TextBox ID="txtqua" runat="server" Text='<%#Eval("quantity_available") %>' Visible="false" Enabled="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit Price">
<ItemTemplate>
<asp:Label ID="lblunt" runat="server" Text='<%#Eval("unit_price") %>'></asp:Label>
<asp:TextBox ID="txtunt" runat="server" Text='<%#Eval("unit_price") %>' Visible="false" Enabled="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnUpdate" OnClick="btnupdate_Click" Visible="false" runat="server" Text="Button" />
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (Session["vendor"] != null)
{
GetGridviewData();
}
}
}
public void GetGridviewData()
{
string[] vendors = Session["vendor"].ToString().Split('\n');
foreach (string vendor in vendors)
{
var data = vendor.Trim();
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
SqlCommand cmd = new SqlCommand("select Id,[Part_number],Nomenclature,[Vendor_Code],quantity,[quantity_available],[unit_price] from Catalouge where [Vendor_Code]=('" + data + "')", conn);
Gridorder.DataSource = this.ExecuteQuery(cmd, "SELECT");
Gridorder.DataBind();
}
}
private DataTable ExecuteQuery(SqlCommand cmd, string action)
{
string conString = (ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
using (SqlConnection con = new SqlConnection(conString))
{
cmd.Connection = con;
switch (action)
{
case "SELECT":
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
case "UPDATE":
con.Open();
cmd.ExecuteNonQuery();
con.Close();
break;
}
return null;
}
}
protected void OnCheckedChanged(object sender, EventArgs e)
{
bool isUpdateVisible = false;
CheckBox chk = (sender as CheckBox);
if (chk.ID == "chkAll")
{
foreach (GridViewRow row in Gridorder.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked = chk.Checked;
}
}
}
CheckBox chkAll = (Gridorder.HeaderRow.FindControl("chkAll") as CheckBox);
chkAll.Checked = true;
foreach (GridViewRow row in Gridorder.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
bool isChecked = row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked;
for (int i = 1; i < row.Cells.Count; i++)
{
row.Cells[i].Controls.OfType<Label>().FirstOrDefault().Visible = !isChecked;
if (row.Cells[i].Controls.OfType<TextBox>().ToList().Count > 0)
{
row.Cells[i].Controls.OfType<TextBox>().FirstOrDefault().Visible = isChecked;
}
if (isChecked && !isUpdateVisible)
{
isUpdateVisible = true;
}
if (!isChecked)
{
chkAll.Checked = false;
}
}
}
}
btnUpdate.Visible = isUpdateVisible;
}
protected void btnupdate_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in Gridorder.Rows)
{
int quan = Convert.ToInt32((row.FindControl("txtqu") as TextBox).Text.Trim());
int avail = Convert.ToInt32((row.FindControl("txtqua") as TextBox).Text.Trim());
if (quan > avail)
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Quantity is greater than Available Quantity');window.location ='order.aspx';", true);
}
else
{
bool isChecked = (row.FindControl("chkSelect") as CheckBox).Checked;
if (isChecked)
{
SqlCommand cmd = new SqlCommand("update Catalouge set quantity=@quantity where Id=@Id ");
cmd.Parameters.AddWithValue("@quantity", quan);
cmd.Parameters.AddWithValue("@Id", Gridorder.DataKeys[row.RowIndex].Value);
this.ExecuteQuery(cmd, "SELECT");
}
btnUpdate.Visible = false;
GetGridviewData();
}
}
}