Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

how to bind search results to datagrid

Posted on 2007-07-29
7
Medium Priority
?
434 Views
Last Modified: 2011-09-20
I have a search form and I need to show the search results and feature the option to update records (based on the chosen record from the returned recordset).  i pulled over a datagrid, 'searchgrid', but  i am not sure how to bind the data to the datagrid, since the data is from the search form.    I know how to just bind based on datasource. how can I  bind the data from the search tot he datagrid?  please advise.  the action begins when the user pushes the search button, so i put the bindgrid function in this event (please excuse the lingo).

here's a sample of what I have:

   protected void searchBtn_Click(object sender, EventArgs e)
    {
        //bind datagrid

            bindsearchGrid();

            //data commented out for testing
/*/if (mysearch.HasRows)
            {
               while (mysearch.Read())
                {
                    taskChoice.Visible = true;
                    searchResults.Visible = true;
                    string bcode = (string)mysearch["Barcode"];
                    string format = (string)mysearch["format"];
                    string show = (string)mysearch["ShowTitle"];
                    string music = (string)mysearch["MusicTitle"];
                    string forma = (string)mysearch["Format"];
                    string biblio = (string)mysearch["Library"];
                    string shelf = (string)mysearch["Shelf"];
                    int shelfnum = (int)mysearch["ShelfNumber"];
                    int record = (int)mysearch["ItemID"];

                    if (bcode != DBNull.Value)

                        iferrorslbl.Text = "Barcode: " + bcode;
                    if (show != DBNull.Value)
                        searchResults.Text = "Show:" + show + "<br>";

                    if (music != DBNull.Value)
                        iferrorslbl.Text = "Video: " + music + "<br>";
                    if (forma != DBNull.Value)
                        searchResults.Text = "Format: " + forma + "<br>";
                    else
                        iferrorslbl.Text = "Format not provided.";

                    if (biblio != DBNull.Value)
                        searchResults.Text = "Library: " + biblio + "<br>";
                    else
                        iferrorslbl.Text = "Library information missing.  You can not make duplicates of this record until the Library information has been updated.";

                    if (shelf != DBNull.Value)
                        searchResults.Text = "Shelf: " + shelf + "<br>";
                    else
                        iferrorslbl.Text = "Shelf Information missing";

                    if (shelfnum != DBNull.Value)
                        searchResults.Text = "Shelf Number: " + shelfnum + "<br>";

                }
            }
           */
//end commented out code
        }
     
    }

    private void bindsearchGrid()
    {
        string bindresults = ConfigurationManager.ConnectionStrings["RevisedTapeLibrary"].ConnectionString;
        SqlConnection conn = new SqlConnection(bindresults);
        DataTable dtresults = new DataTable();

        int myLib = Convert.ToInt32(searchlib.SelectedValue);
        int myformat = Convert.ToInt32(searchformat.SelectedValue);
        string mycodes = Convert.ToString(bcodetxtbx.Text);

        SqlDataAdapter myadap = new SqlDataAdapter("select * from view_barcodes WHERE barcode = " + mycodes + "or format=" + myformat + "or library = " + myLib, conn);
        taskChoice.Visible = true;
        myadap.Fill(dtresults, searchGrid);

        searchGrid.DataSource = Source;
        searchGrid.DataBind();      
    }

0
Comment
Question by:synergeticsoul
  • 3
  • 3
7 Comments
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 19590189
can u explain a bit more .. this is not quite clear
by what i understand
 u can directly bind ur serach datasource to the
datagrid
..
lke MainGrid.DataSource = serach.Datasource;


0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 19590610
Just out of interest, are you not wanting to use the datatable you fill as the datasource, e.g.

    private void bindsearchGrid()
    {
        string bindresults = ConfigurationManager.ConnectionStrings["RevisedTapeLibrary"].ConnectionString;
        SqlConnection conn = new SqlConnection(bindresults);
        DataTable dtresults = new DataTable();

        int myLib = Convert.ToInt32(searchlib.SelectedValue);
        int myformat = Convert.ToInt32(searchformat.SelectedValue);
        string mycodes = Convert.ToString(bcodetxtbx.Text);

        SqlDataAdapter myadap = new SqlDataAdapter("select * from view_barcodes WHERE barcode = " + mycodes + "or format=" + myformat + "or library = " + myLib, conn);
        taskChoice.Visible = true;
        myadap.Fill(dtresults, searchGrid);

        searchGrid.DataSource = dtresults.DefaultView;
        searchGrid.DataBind();      
    }
0
 

Author Comment

by:synergeticsoul
ID: 19592173
thankyou both.  I wasn't aware I could use the datatable.  I am filling the datatable, but I need to have the records populate a grid (I guess), so that I can put the radiocontrols in an itemtemplate and enable the user to select the records they want to modify.

using the datatable would work the same as using the datagrid?  how would i use the datatable in place of the gridview and still maintain the same functionality.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 19592392
Your DataGrid is used to display the data.  

You can use the DataTable or the DataTables Default View or a DataView of your own design as the source of the data.

Your can then enable bindings on controls inside the datagrid to give the desired effect.

You should read up on the events of the DataGird also.

Andrew


0
 

Author Comment

by:synergeticsoul
ID: 19592626
wow.  I have successfully put bound the data to a datagrid.  is there a way to switch the items from my datagrid around?  I have put a radio buttonlist in my itemtemplate, but since I have the grid selected to autogenerate fields, is there a way to move this to the last column?
0
 
LVL 20

Accepted Solution

by:
REA_ANDREW earned 1000 total points
ID: 19593360
take off the auto generated fields and add your own in the code

Andrew
0
 

Author Comment

by:synergeticsoul
ID: 19594710
hmmm...ok....thanks.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month15 days, 9 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question