Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Gridview with checkbox column

Gridview with checkbox column

Scheduled Pinned Locked Moved ASP.NET
helpquestion
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anupbala
    wrote on last edited by
    #1

    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

    N B E 3 Replies Last reply
    0
    • A Anupbala

      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

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      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

      A 1 Reply Last reply
      0
      • N N a v a n e e t h

        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

        A Offline
        A Offline
        Anupbala
        wrote on last edited by
        #3

        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.

        N 1 Reply Last reply
        0
        • A Anupbala

          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.

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          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

          A 1 Reply Last reply
          0
          • N N a v a n e e t h

            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

            A Offline
            A Offline
            Anupbala
            wrote on last edited by
            #5

            DataTable.Rows.Add(row) cannot be used since we cannot add an existing row to another datatable. i have used dt1 = dt.Clone() dt1.ImportRow(row) but it there i'll get only the last row.I want all the selected row.Please help

            1 Reply Last reply
            0
            • A Anupbala

              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

              B Offline
              B Offline
              bokuceres
              wrote on last edited by
              #6

              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.

              A 1 Reply Last reply
              0
              • B bokuceres

                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.

                A Offline
                A Offline
                Anupbala
                wrote on last edited by
                #7

                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

                B 1 Reply Last reply
                0
                • A Anupbala

                  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

                  E Offline
                  E Offline
                  Elayaraja Sambasivam
                  wrote on last edited by
                  #8

                  remove the gridviewrow from the gv1.rows, which are the rows are not selected.

                  1 Reply Last reply
                  0
                  • A Anupbala

                    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

                    B Offline
                    B Offline
                    bokuceres
                    wrote on last edited by
                    #9

                    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

                    A 1 Reply Last reply
                    0
                    • B bokuceres

                      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

                      A Offline
                      A Offline
                      Anupbala
                      wrote on last edited by
                      #10

                      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

                      B 1 Reply Last reply
                      0
                      • A Anupbala

                        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

                        B Offline
                        B Offline
                        bokuceres
                        wrote on last edited by
                        #11

                        oh ok dude... asp.net forum rocks... hehehe

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups