hi am using a web form in vs2010 and in my form am having a gridview with button on footer.i have written the code to create a new row while click the button.if i clcik the button i got the following error "unable to cast object of type 'system.web.ui.webcontrols.textbox' to type 'mysite.textbox"
can anyone help me to solve this issue.
below is the button click code.
private void AddNewRow()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
TextBox txtcontact = (TextBox)grvDetails.Rows[rowIndex].Cells[1].FindControl("txtcontact");
TextBox txtdesig = (TextBox)grvDetails.Rows[rowIndex].Cells[2].FindControl("txtdesig");
TextBox txtpno = (TextBox)grvDetails.Rows[rowIndex].Cells[3].FindControl("txtpno");
TextBox txtmob = (TextBox)grvDetails.Rows[rowIndex].Cells[4].FindControl("txtmob");
TextBox txtemail = (TextBox)grvDetails.Rows[rowIndex].Cells[5].FindControl("txtemail");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
dtCurrentTable.Rows[i - 1]["Col1"] = txtcontact.Text;
dtCurrentTable.Rows[i - 1]["Col2"] = txtdesig.Text;
dtCurrentTable.Rows[i - 1]["Col3"] = txtpno.Text;
dtCurrentTable.Rows[i - 1]["Col4"] = txtmob.Text;
dtCurrentTable.Rows[i - 1]["Col5"] = txtemail.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
grvDetails.DataSource = dtCurrentTable;
grvDetails.DataBind();
int j = grvDetails.Rows.Count - 1;
TextBox txn = (TextBox)grvDetails.Rows[j].Cells[1].FindControl("txtcontact");
txn.Focus();
}
}
else
{
}
}