When i am inserting data into gridview then below error is coming
Unable to cast object of type 'System.Web.UI.WebControls.GridViewRow' to type 'System.Data.DataRow'.
here is my insert query in gridview
protected void btnadd_Click(object sender, EventArgs e)
{
insertbalgv();
}
private void insertbalgv()
{
if (DDLitem.SelectedItem != null)
{
int codeitem = Convert.ToInt32(DDLitem.SelectedItem.Value.ToString());
string Descriptionitem = DDLitem.SelectedItem.Text.ToString();
DataTable dtgridview = ViewState["dt"] as DataTable;
DataRow dr2 = dtgridview.NewRow();
bool ifExist = false;
foreach (DataRow dr in GVbal.Rows)
{
if (dr["BID"].ToString() == txtsearcbn.Text.Trim())
{
ifExist = true;
break;
}
}
if (!ifExist)
{
dr2["BID"] = txtsearcbn.Text.Trim();
dr2["Codeitem"] = codeitem;
dr2["Descriptionitem"] = Descriptionitem;
dr2["QTY"] = txtqty.Text;
dtgridview.Rows.Add(dr2);
ViewState["dt"] = dtgridview;
GVbal.DataSource = dtgridview;
GVbal.DataBind();
}
else
{
// this.lbgvck.Visible = true;
Response.Write("<script language='javascript'>alert('Bale Already Added');</script>");
}
clear();
}
}
private void clear()
{
txtsearcbn.Text = "";
}
}