Gridview with checkbox column
-
Hi, I am having a gridview with a checkbox column.i want to export the selected rows which are checked to a word document.I have done the code to export to word document.There were other 2 options which are already complete.There is option to export each row seperately to word document and export the entire rows from gridview to word document. This is a new requirement to export selected rows to word document. i have done this much int count = 0; foreach (GridViewRow gv in gv1.Rows) { CheckBox cb = (CheckBox)gv.FindControl("chkSelect"); if (cb.Checked) { count++; } } I was able to get the count of the selected rows.How can i take the selected recordsand export to a word document.I need the sample code of that part.This is very urgent.Please help. Thanks Anup
-
Hi, I am having a gridview with a checkbox column.i want to export the selected rows which are checked to a word document.I have done the code to export to word document.There were other 2 options which are already complete.There is option to export each row seperately to word document and export the entire rows from gridview to word document. This is a new requirement to export selected rows to word document. i have done this much int count = 0; foreach (GridViewRow gv in gv1.Rows) { CheckBox cb = (CheckBox)gv.FindControl("chkSelect"); if (cb.Checked) { count++; } } I was able to get the count of the selected rows.How can i take the selected recordsand export to a word document.I need the sample code of that part.This is very urgent.Please help. Thanks Anup
What is Gridbiew's data source type ? I assume it's a DataTable. So you could do the following.
DataTable dt = (DataTable)gv.DataSource;
int count = 0;
foreach (GridViewRow gv in gv1.Rows)
{
CheckBox cb = (CheckBox)gv.FindControl("chkSelect");
if (cb.Checked)
{
DataRow row = dt.Rows[count];
//Take necessary values from row
count++;
}
}All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions
-
What is Gridbiew's data source type ? I assume it's a DataTable. So you could do the following.
DataTable dt = (DataTable)gv.DataSource;
int count = 0;
foreach (GridViewRow gv in gv1.Rows)
{
CheckBox cb = (CheckBox)gv.FindControl("chkSelect");
if (cb.Checked)
{
DataRow row = dt.Rows[count];
//Take necessary values from row
count++;
}
}All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions
-
Gridbiew's data source is datatable as you said. I want to export the selected row to a word document.If i have selected 10 rows how can i get that rows and assign to another datatable.
Create a new DataTable instance and add this rows to there like
DataTable.Rows.Add(row)
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions
-
Create a new DataTable instance and add this rows to there like
DataTable.Rows.Add(row)
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions
-
Hi, I am having a gridview with a checkbox column.i want to export the selected rows which are checked to a word document.I have done the code to export to word document.There were other 2 options which are already complete.There is option to export each row seperately to word document and export the entire rows from gridview to word document. This is a new requirement to export selected rows to word document. i have done this much int count = 0; foreach (GridViewRow gv in gv1.Rows) { CheckBox cb = (CheckBox)gv.FindControl("chkSelect"); if (cb.Checked) { count++; } } I was able to get the count of the selected rows.How can i take the selected recordsand export to a word document.I need the sample code of that part.This is very urgent.Please help. Thanks Anup
is there any private key or unique table's field in the record? if there is any unique field in data that you bind to gridview than assign it to hiddenfield control in your gridview (like you put checkbox control in your gridview).
Arraylist arrID = new arraylist();
foreach (GridViewRow gv in gv1.Rows)
{
CheckBox cb = (CheckBox)gv.FindControl("chkSelect");
if (cb.Checked)
{
HiddenField hf = (HiddenField)gv.FindControl("HiddenFieldNames");
arrID.add(hf.value);
}
}DataTable dbTable = QueryBackToDB(arrID);
then you can query arrID to your database then ..... walah...there's your selected record (dbTable in code example, above). this will query back to your database again, but at least, the data will be reliable. you can always try to get the index where your checkbox is checked try to loop with [for], and don't with [for each] so you can get the index easily. then you match with the index from your search with the index in datatable that you bind to gridview. i hope this can help you.
-
is there any private key or unique table's field in the record? if there is any unique field in data that you bind to gridview than assign it to hiddenfield control in your gridview (like you put checkbox control in your gridview).
Arraylist arrID = new arraylist();
foreach (GridViewRow gv in gv1.Rows)
{
CheckBox cb = (CheckBox)gv.FindControl("chkSelect");
if (cb.Checked)
{
HiddenField hf = (HiddenField)gv.FindControl("HiddenFieldNames");
arrID.add(hf.value);
}
}DataTable dbTable = QueryBackToDB(arrID);
then you can query arrID to your database then ..... walah...there's your selected record (dbTable in code example, above). this will query back to your database again, but at least, the data will be reliable. you can always try to get the index where your checkbox is checked try to loop with [for], and don't with [for each] so you can get the index easily. then you match with the index from your search with the index in datatable that you bind to gridview. i hope this can help you.
I have already done what you have told,i am already having all the records in datable so need to query it again from database. here is what i have done DataTable dt = searchDatatable(); DataTable dt1 = new DataTable(); int count = 0; DataRow row=null; foreach (GridViewRow gv in gv1.Rows) { CheckBox cb = (CheckBox)gv.FindControl("chkSelect"); if (cb.Checked) { row = dt.Rows[count]; //Take necessary values from row dt1 = dt.Clone(); count++; foreach (DataRow dr in dt.Rows) { dt1.ImportRow(dr); } } } this will return only the last row,i want all the rows which r checked.I have spend a long time for this and its very urgent. Please help
-
Hi, I am having a gridview with a checkbox column.i want to export the selected rows which are checked to a word document.I have done the code to export to word document.There were other 2 options which are already complete.There is option to export each row seperately to word document and export the entire rows from gridview to word document. This is a new requirement to export selected rows to word document. i have done this much int count = 0; foreach (GridViewRow gv in gv1.Rows) { CheckBox cb = (CheckBox)gv.FindControl("chkSelect"); if (cb.Checked) { count++; } } I was able to get the count of the selected rows.How can i take the selected recordsand export to a word document.I need the sample code of that part.This is very urgent.Please help. Thanks Anup
remove the gridviewrow from the gv1.rows, which are the rows are not selected.
-
I have already done what you have told,i am already having all the records in datable so need to query it again from database. here is what i have done DataTable dt = searchDatatable(); DataTable dt1 = new DataTable(); int count = 0; DataRow row=null; foreach (GridViewRow gv in gv1.Rows) { CheckBox cb = (CheckBox)gv.FindControl("chkSelect"); if (cb.Checked) { row = dt.Rows[count]; //Take necessary values from row dt1 = dt.Clone(); count++; foreach (DataRow dr in dt.Rows) { dt1.ImportRow(dr); } } } this will return only the last row,i want all the rows which r checked.I have spend a long time for this and its very urgent. Please help
hello Anupbala, i'm sorry that i can't reply ASAP due yesterday i have employee meeting. have you find the solution? if not, you didn't do exactly what i have told you, if i look in to your code. you need to do the process individually. don't grouping them in one looping process. this what i mean :
Arraylist arrDataToQuery = GetAllTheIDWhenCheckboxIsChecked();
Datatable dtQueryResult = QueryResult(arrDataToQuery);
/*
in query result, you can query with the id with [IN] keyword
example : Select * From City Where CityID In ('1','2','3')
all you have to do is built algorithm to built string like ('1','2','3') from the arraylist that contain id.
*/gvDontCareItsName.DataSource = dtQueryResult;
gvDontCareItsName.Databind();hope this help, if u're not dead with this problem ;P , or maybe you have won... :-D
-
hello Anupbala, i'm sorry that i can't reply ASAP due yesterday i have employee meeting. have you find the solution? if not, you didn't do exactly what i have told you, if i look in to your code. you need to do the process individually. don't grouping them in one looping process. this what i mean :
Arraylist arrDataToQuery = GetAllTheIDWhenCheckboxIsChecked();
Datatable dtQueryResult = QueryResult(arrDataToQuery);
/*
in query result, you can query with the id with [IN] keyword
example : Select * From City Where CityID In ('1','2','3')
all you have to do is built algorithm to built string like ('1','2','3') from the arraylist that contain id.
*/gvDontCareItsName.DataSource = dtQueryResult;
gvDontCareItsName.Databind();hope this help, if u're not dead with this problem ;P , or maybe you have won... :-D
Hi Boku, Thanks for your response,i have found a solution for this in the other way as i said. i am already having the entire records in a datatable,so there is no need to query it back again. here is my solution DataTable dt = GetAllRows(); DataTable dt1 = dt.Clone(); int count = 0; DataRow row = null; foreach (GridViewRow gv in gv1.Rows) { CheckBox cb = (CheckBox)gv.FindControl("chkSelect"); if (cb.Checked) { row = dt.Rows[gv.RowIndex]; dt1.ImportRow(row); count++; } } dt1.AcceptChanges(); I am posting it as it comes helpful for somebody else:-D
-
Hi Boku, Thanks for your response,i have found a solution for this in the other way as i said. i am already having the entire records in a datatable,so there is no need to query it back again. here is my solution DataTable dt = GetAllRows(); DataTable dt1 = dt.Clone(); int count = 0; DataRow row = null; foreach (GridViewRow gv in gv1.Rows) { CheckBox cb = (CheckBox)gv.FindControl("chkSelect"); if (cb.Checked) { row = dt.Rows[gv.RowIndex]; dt1.ImportRow(row); count++; } } dt1.AcceptChanges(); I am posting it as it comes helpful for somebody else:-D