Exporting a grid view to datatable - asp.net

How can i export a .net GridView into a DataTable.
I am not setting any data source to the GridView all data are entered by user...
If any body knows please help me..thanks in advance....

The Following Code will help you in adding the Gridview rows to DataTable
DataTable dt = new DataTable();
for (int j = 0; j < grdList.Rows.Count; j++)
{
DataRow dr;
GridViewRow row = grdList.Rows[j];
dr = dt.NewRow();
for (int i = 0; i < row.Cells.Count; i++)
{
dr[i] = row.Cells[i].Text;
}
dt.Rows.Add(dr);
}

Related

can't add all the selected rows to the dataset

I have added records chosen (checkbox) in a datatable shown below, records have been displaying in a datagrid populated using stored procedure. Then on the button click I save all the selected (added) records to the ds in database.
public DataSet getMain()
{
DataSet ds = CFObj.GetSchemaRequest();
DataRow dr;
dr = ds.Tables[0].NewRow();
for (int i = 0; i < dgAvailableCandidates.Items.Count; i++)
{
CheckBox saveChkBoxItem = (CheckBox)dgAvailableCandidates.Items[i].FindControl("chkbox_SelectCandidate");
if (saveChkBoxItem.Checked)
{
//ds.Tables[0].Columns["pk_scrndRecId"].ReadOnly = false;
dr["pk_scrndRecId"] = "0";
dr["fk_jobId"] = ddl_jobList.SelectedValue.ToString();
obj._fkjob_Id = ddl_jobList.SelectedValue.ToString();
dr["fk_recId"] = dgAvailableCandidates.DataKeys[i].ToString();
obj._fk_recId = dgAvailableCandidates.DataKeys[i].ToString();
}
}
ds.Tables[0].Rows.Add(dr);
return ds;
}
here my issue is when ds.Tables[0].Rows.Add(dr); i put outside of loop, it adds only one last selected record to ds, and when I put this inside loop, it throws following exception
[pk_scrndRecId] column is read only
and when I have placed this inside if block, it throws following exception:
This row already belongs to this table.
I am not able to understand why this code doesn't add all the selected rows of datagrid to the ds.
please help. thanks!
You have to create a new row and add it to datatable inside the loop and so each time it will create a new instance and add it to your datatable.
Check updated code.
public DataSet getMain()
{
DataSet ds = CFObj.GetSchemaRequest();
DataRow dr;
for (int i = 0; i < dgAvailableCandidates.Items.Count; i++)
{
CheckBox saveChkBoxItem = (CheckBox)dgAvailableCandidates.Items[i].FindControl("chkbox_SelectCandidate");
if (saveChkBoxItem.Checked)
{
dr = ds.Tables[0].NewRow();
//ds.Tables[0].Columns["pk_scrndRecId"].ReadOnly = false;
dr["pk_scrndRecId"] = "0";
dr["fk_jobId"] = ddl_jobList.SelectedValue.ToString();
obj._fkjob_Id = ddl_jobList.SelectedValue.ToString();
dr["fk_recId"] = dgAvailableCandidates.DataKeys[i].ToString();
obj._fk_recId = dgAvailableCandidates.DataKeys[i].ToString();
ds.Tables[0].Rows.Add(dr);
}
}
return ds;
}

how to find control of static control without for loop

private void searchgrid()
{
GridViewRow HeaderGridRow = new GridViewRow(0, 1, DataControlRowType.Header, DataControlRowState.Normal);
TableCell HeaderCell = new TableCell();
HeaderCell.Text = "";
TextBox abc = new TextBox();
HeaderCell.Controls.Add(abc);
HeaderCell.ColumnSpan = 3;
HeaderGridRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.Text = "";
TextBox search = new TextBox();
search.ID = "search1";
HeaderCell.Controls.Add(search);
HeaderCell.ColumnSpan = 3;
HeaderGridRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
Button btn_chk = new Button();
btn_chk.Text = "go";
HeaderCell.Controls.Add(btn_chk);
HeaderCell.ColumnSpan = 1;
btn_chk.Click += new EventHandler(btnClick);
HeaderGridRow.Cells.Add(HeaderCell);
//tbl1.Controls.Add(HeaderGridRow);
//btn_chk.CommandName = "btn_chk";
GridView2.Controls[0].Controls.AddAt(0, HeaderGridRow);
//GridView2.Controls[0].Controls.AddAt(0, tbl1);
}
i have a gridview,inside this there is another dynamically created gridview which hAS a textbox.how to find control of this textbox.by looping i get the values but i don get the desired output..so i need just one textbox value..please help
You can do it without a loop like this:
GridView gv = GridView1.Rows[0].FindControl("GridView2") as GridView;
TextBox tb = gv.Rows[0].FindControl("TextBox1") as TextBox;
string tbValue = tb.Text;
You find the nested GridView first and cast it, then you can find the TextBox in the nested GridView.
And you could still always use a loop:
foreach (GridViewRow parentRow in GridView1.Rows)
{
if (parentRow.RowType == DataControlRowType.DataRow)
{
GridView gv = (GridView)parentRow.FindControl("GridView2");
foreach (GridViewRow childRow in gv.Rows)
{
if (childRow.RowType == DataControlRowType.DataRow)
{
TextBox tb = (TextBox)childRow.FindControl("TextBox1");
string tbValue = tb.Text;
}
}
}
}

Import CSV file into Grid View in Asp.net

How to load the CSV file dynamically inside Grid View in C# Asp.net without hard coded values?
if (File.Exists(myFileUpload.PostedFile.FileName))
{
string[] data = File.ReadAllLines(myFileUpload.PostedFile.FileName);
DataTable dt = new DataTable();
string[] col = data[0].Split(',');
foreach (string s in col)
{
dt.Columns.Add(s, typeof(string));
}
for (int i = 0; i < data.Length; i++)
{
string[] row = data[i].Split(',');
dt.Rows.Add(row);
}
myGridView.DataSource = dt;
myGridView.DataBind();
}

generate Dynamic Checkbox in ASP.Net

plz tell me whats wrong in code bcz i m not able to add control on page. i m getting the values right & if i just CheckBoxList2.Items.Add(row["subj_nme"].ToString()); the check box get created
if (ds.Tables.Count > 0)
{
foreach (DataRow row in ds.Tables[0].Rows)
{
chkList1 = new CheckBox();
chkList1.Text = row["subj_nme"].ToString();
chkList1.ID = row["subjid"].ToString();
chkList1.Checked = true;
chkList1.Font.Name = "Verdana";
chkList1.Font.Size = 12;
CheckBoxList2.Controls.Add(chkList1);
}
}
I think you can use this code for binding your CheckBoxList2 to DataTable as follow.
CheckBoxList2.DataSource = ds.Tables[0];
CheckBoxList2.DataTextField = "subj_nme";
CheckBoxList2.DataValueField = "subjid";
CheckBoxList2.DataBind();
CheckBoxList2.Font.Name = "Verdana";
CheckBoxList2.Font.Size = 12;
To check them all you can do this
for(int i=0;i<CheckBoxList2.Items.Count;i++)
{
CheckBoxList2.Items[i].Selected = true;
}

Get Id of dynamically added control

i have one html table "table "and added controls it at run time as :
HtmlTableRow tabelrow = new HtmlTableRow();
HtmlTableCell tablecell = new HtmlTableCell();
Label lbl = new Label();
tablecell.Controls.Add(lbl);
then i want to get each controls by using foreach
such as (foreach control c in table.controls) or (foreach control c in tablecell.controls)
but its not working.
Tried the following. I can get the controls with the ID but you need to assign an ID to it first.
HtmlTable table = new HtmlTable();
for (int i = 0; i < 10; i++)
{
HtmlTableRow tablerow = new HtmlTableRow();
tablerow.ID = Guid.NewGuid().ToString();
HtmlTableCell tablecell = new HtmlTableCell();
tablecell.ID = Guid.NewGuid().ToString();
Label lbl = new Label();
lbl.ID = Guid.NewGuid().ToString();
tablecell.Controls.Add(lbl);
tablerow.Controls.Add(tablecell);
table.Controls.Add(tablerow);
}
List<string>RowID = new List<string>();
List<string>CellID = new List<string>();
List<string>LabelID = new List<string>();
foreach (Control Row in table.Controls)
{
//Each control will be a tablerow.
RowID.Add(Row.ID);
CellID.Add(Row.Controls[0].ID);
LabelID.Add(Row.Controls[0].Controls[0].ID);
}
What exactly are you trying to do? Please elaborate.

Resources