Wrox Programmer Forums
|
BOOK: ASP.NET Website Programming Problem-Design-Solution
This is the forum to discuss the Wrox book ASP.NET Website Programming: Problem - Design - Solution, Visual Basic .NET Edition by Marco Bellinaso, Kevin Hoffman; ISBN: 9780764543869
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET Website Programming Problem-Design-Solution section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 13th, 2004, 02:16 AM
Authorized User
 
Join Date: Aug 2004
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to macupryk Send a message via Yahoo to macupryk
Default Help with a DataGrid?

How do I bind a dropdownlist to a multiple table query that is

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE PROCEDURE [dbo].[Users_Select] AS SELECT
      tbl_Users.UserID,
      tbl_Users.FullName,
      tbl_Users.UserName,
      tbl_Users.ReportToID,
      tbl_Users_AccessLevelID.AccessLevel,
      tbl_Users_StoreID.Store,
      tbl_Users.UserID FROM tbl_Users_AccessLevelID
INNER JOIN tbl_Users ON (tbl_Users_AccessLevelID.AccessLevelID = tbl_Users.AccessLevelID)
INNER JOIN tbl_Users_StoreID ON (tbl_Users.StoreID = tbl_Users_StoreID.StoreID)

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO


Where Store is 1 = > name1
           2 = > name2
           3 = > name3

     In the dropdownlist the values should be name1 - name3


    public class Webform : System.Web.UI.Page
    {
        protected DataView TempDataView = new DataView();
        protected System.Web.UI.WebControls.DataGrid DataGrid1;
        // Only run this code the first time the page is loaded.
        // The code inside the IF statement is skipped when you resubmit the page.


        private void Page_Load(object sender, System.EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGrid();

            }

        }

        public void EditDataGrid(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            DataGrid1.EditItemIndex = e.Item.ItemIndex;
            BindGrid();
        }

        public void CancelDataGrid(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            DataGrid1.EditItemIndex = -1;
            BindGrid();
        }

        public void UpdateDataGrid(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            // Display information about changes
            DropDownList ddlStore = (DropDownList) e.Item.FindControl("ddlStore");
            Response.Write("<h2>You selected " + ddlStore.SelectedItem.Text + " (" + ddlStore.SelectedValue + ")</h2>");

            DataGrid1.EditItemIndex = -1;
            BindGrid();
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHand ler(this.CancelDataGrid);
            this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHand ler(this.EditDataGrid);
            this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHand ler(this.UpdateDataGrid);
            this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler (this.DataGrid1_ItemDataBound);
            this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexCh anged);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion



        public void BindGrid()
        {
            // Put user code to initialize the page here
            //Create a connection to the SQL Server; modify the connection string for your environment.
            SqlConnection MyConnection = new SqlConnection("server=(local);database=MSPOS;UID=s a;PWD=;");

            //Create a DataAdapter, and then provide the name of the stored procedure.
            SqlDataAdapter MyDataAdapter = new SqlDataAdapter("prc_Select_Users", MyConnection);

            //Set the command type as StoredProcedure.
            MyDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
            MyConnection.Open();
            MyDataAdapter.SelectCommand.ExecuteNonQuery();
            DataSet ds = new DataSet();
            //Fill the DataSet with the rows that are returned.
            MyDataAdapter.Fill(ds, "prc_Select_Users");
            //Set the data source for the DataGrid as the DataSet that holds the rows.
            DataGrid1.DataSource = ds.Tables["prc_Select_Users"].DefaultView;
            //NOTE: If you do not call this method, the DataGrid is not displayed!
            DataGrid1.DataBind();

            MyDataAdapter.Dispose(); //Dispose the DataAdapter.
            MyConnection.Close(); //Close the connection.
        }

        private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {

        }


        private void MyDataGrid_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList list = (DropDownList)sender;

            TableCell cell = list.Parent as TableCell;
            DataGridItem item = cell.Parent as DataGridItem;

            int index = item.ItemIndex;
            string content = item.Cells[0].Text;

            Response.Write(
                String.Format("Row {0} contains {1}", index, content)
                );

        }

        private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
        {

        }


I am having a heck of a time with this, If I am in the wrong place please direct me to the right one. If you know how to bind the dropdownlist to a a multiple table quey could you please tell me. I would appreciate it .

Sincerely,

Mathieu Cupryk

Knowledge is to be shared.
__________________
Knowledge is to be shared.
 
Old September 13th, 2004, 07:26 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

You are using that query solely for the datagrid grid, but would like to kill two birds with one stone, I imagine? At any rate, I don't think that is going to be the best solution, as you can't select a distinct query set in ADO.NET code (not yet anyway, you can in 2.0).

Use a separate query if you want to pull the results from the database. You don't need to do that in the BindGrid, just do it once in the page load event.

Brian





Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom paging in Datagrid with datagrid page count madhusrp ASP.NET 1.0 and 1.1 Professional 12 June 2nd, 2008 01:15 PM
User COntrol Datagrid inside datagrid rodmcleay ASP.NET 1.0 and 1.1 Professional 3 April 14th, 2007 10:11 AM
Creating DataGrid In Repeater/DataGrid liduwan ASP.NET 1.0 and 1.1 Professional 9 March 6th, 2007 03:31 PM
datagrid ajaidass ADO.NET 2 March 1st, 2007 05:36 PM
Need help regarding datagrid ngsharmila .NET Framework 1.x 1 February 1st, 2006 12:55 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.