Hi all,
I am converting grid view to data table using the below code.
public DataTable GetDataTable(GridView dtg)
{
try
{
DataTable dt = new DataTable();
// add the columns to the datatable
if (dtg.HeaderRow != null)
{
for (int i = 0; i < dtg.HeaderRow.Cells.Count; i++)
{
dt.Columns.Add(dtg.HeaderRow.Cells[i].Text);
}
}
// add each of the data rows to the table
foreach (GridViewRow row in dtg.Rows)
{
DataRow dr;
dr = dt.NewRow();
Literal l = new Literal();
for (int i = 0; i < row.Cells.Count; i++)
{
if (row.Cells[i].HasControls())
{
if (row.Cells[i].GetType() == typeof(LinkButton))
{
l.Text = (row.Cells[i].Controls[1] as LinkButton).Text;
dr[i] = l.Text.ToString();
}
if (row.Cells[i].GetType() == typeof(HyperLink))
{
l.Text = (row.Cells[i].Controls[1] as HyperLink).Text;
dr[i] = l.Text.ToString();
}
else
{
l.Text = (row.Cells[i].Controls[1] as Label).Text;
dr[i] = l.Text.ToString();
}
}
else
{
dr[i] = row.Cells[i].Text.Replace(" ", "");
}
}
dt.Rows.Add(dr);
}
return dt;
}
catch (Exception ex)
{
throw ex;
}
}
My problem is if grid view has controls like link button ,label i am using has controls property and reading that text.
I am passing grid view which contains link buttons it is giving an error object reference is not set instance of object.when i am debugging the code i identified that its not going to link button condition. but its going to inside has controls method.
My main theme is converting grid view to data table even it contains controls
Please help me.
Thanks in advance
Regards,
Bhanu
Regards,
Bhanu Prakash Bysani