Click here to Skip to main content
15,918,808 members
Home / Discussions / C#
   

C#

 
AnswerRe: creating DSN for mySQL Pin
led mike10-Aug-06 10:40
led mike10-Aug-06 10:40 
QuestionDebugging c# in VS2005 - Break When Value Changes Pin
bearfx10-Aug-06 10:07
bearfx10-Aug-06 10:07 
AnswerRe: Debugging c# in VS2005 - Break When Value Changes Pin
led mike10-Aug-06 10:14
led mike10-Aug-06 10:14 
AnswerRe: Debugging c# in VS2005 - Break When Value Changes Pin
Christian Graus10-Aug-06 12:21
protectorChristian Graus10-Aug-06 12:21 
QuestionDataGrid column, row, and cell styles Pin
spin vector10-Aug-06 9:52
spin vector10-Aug-06 9:52 
AnswerRe: DataGrid column, row, and cell styles Pin
kalyanPaladugu10-Aug-06 12:11
kalyanPaladugu10-Aug-06 12:11 
GeneralRe: DataGrid column, row, and cell styles Pin
spin vector11-Aug-06 1:20
spin vector11-Aug-06 1:20 
QuestionData Display dilemma [modified] Pin
sykiemikey10-Aug-06 8:26
sykiemikey10-Aug-06 8:26 
I'm having trouble figuring out just how I should best approach this.

I have a simple form to edit users

Search fields
DataGridView(view only, no editing)
Edit fields

What I have set up is that when the search is run, 2 datatables are created....1 with slimmed down columns and usable column aliases to be displayed in the datagridview "grid view table", and another that has all columns and data "bulk data table". Both have the same WHERE statements, just different SELECTed columns.

When the enduser clicks on a row in the grid view, the index is pulled from the grid view and used to create a user object from the "bulk data table" which in turn fills all my edit fields with that user's information.

I got this all working correctly until I realized that I could not turn off sorting by header in the grid view, which in turn throws off my indexes to the "bulk data".

I would love for them to be able to sort by the columns, but not at the expense of my whole form not working.

Please let me know if you have any recommendations.
Below is my partial class for the form.
Some of the comments are things I have tried. Below, I was trying to use views to no avail.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace PSTMGR
{
    public partial class AdminControl : UserControl
    {
        IniReader INI;
        //DataTable dispTable;
        DataTable dataTable;
        DataView dataView;
        DataRow dataRow;
        UserObj currentUser;

        public AdminControl()
        {
            InitializeComponent();
            INI = new IniReader(Application.StartupPath + "\\PSTMGR.INI");
            this.DBPath.Text = INI.ReadString("DBInfo","DBPath", "");
        }

        private void changeDBPath_Click(object sender, EventArgs e)
        {
            OFD = InitOFD(null);
            DialogResult dr = OFD.ShowDialog(this);
            if (dr != DialogResult.OK)
                return;

            string fn = OFD.FileName;
            this.DBPath.Text = fn;
            INI.Write("DBInfo", "DBPath", fn);
        }

        private OpenFileDialog InitOFD(OpenFileDialog o)
        {
            if (o != null)
                return o;

            o = new OpenFileDialog();
            o.InitialDirectory = "C:\\";
            o.Title = "Load Image File";
            o.CheckPathExists = true;
            o.CheckFileExists = true;

            //	create filter
            o.Filter = "Access DB(*.MDB)"
                + "|*.MDB";

            return o;
        }

        private void searchUsers_Click(object sender, EventArgs e)
        {
            bool first = true;

            String dataQuery = "SELECT * FROM USERS";
            //String dispQuery = "SELECT FIRST_NAME AS [First Name], " +
            //                   "LAST_NAME AS [Last Name], USER_NAME AS [Username], " +
            //                  "PASSWORD AS [Password] FROM USERS";
            if (searchUserName.Text.Length != 0)
            {
                first = false;
                dataQuery += " WHERE USER_NAME = '" + searchUserName.Text + "'";
            //    dispQuery += " WHERE USER_NAME = '" + searchUserName.Text + "'";
            }
            if (searchFirstName.Text.Length != 0)
            {
                if (first)
                {
                    first = false;
                    dataQuery += " WHERE";
              //      dispQuery += " WHERE";
                }
                else
                {
                    dataQuery += " AND";
                //    dispQuery += " AND";
                }

                dataQuery += " FIRST_NAME = '" + searchFirstName.Text + "'";
                //dispQuery += " FIRST_NAME = '" + searchFirstName.Text + "'";
            }
            if (searchLastName.Text.Length != 0)
            {
                if (first)
                {
                    first = false;
                    dataQuery += " WHERE";
                  //  dispQuery += " WHERE";
                }
                else
                {
                    dataQuery += " AND";
                    //dispQuery += " AND";
                }

                dataQuery += " LAST_NAME = '" + searchLastName.Text + "'";
                //dispQuery += " LAST_NAME = '" + searchLastName.Text + "'";
            }

            dataTable = PSTMGRApp.Connection.Query(dataQuery);
            //dispTable = PSTMGRApp.Connection.Query(dispQuery);
            dataView = new DataView(dataTable);
            this.adminResults.DataSource = dataView;
            fillDataFields();
        }

        private void adminResults_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            fillDataFields();
        }

        private void fillDataFields()
        {
            if (dataTable.Rows.Count != 0)
            {
                //dataRow = dataTable.DefaultView.
                dataRow = dataTable.Rows[adminResults.CurrentRow.Index];

                currentUser = new UserObj(Convert.ToInt32(dataRow["USER_ID"].ToString()),
                                          dataRow["USER_NAME"].ToString(),
                                          dataRow["PASSWORD"].ToString(),
                                          dataRow["FIRST_NAME"].ToString(),
                                          dataRow["LAST_NAME"].ToString(),
                                          Convert.ToBoolean(dataRow["ADMIN_PRIV"]),
                                          Convert.ToBoolean(dataRow["ADD_PRIV"]),
                                          Convert.ToBoolean(dataRow["EDIT_PRIV"]),
                                          Convert.ToBoolean(dataRow["DELETE_PRIV"]));

                userName.Text = currentUser.UserName;
                passWord.Text = currentUser.Password;
                firstName.Text = currentUser.FirstName;
                lastName.Text = currentUser.LastName;
                adminCheck.Checked = currentUser.Admin;
                addCheck.Checked = currentUser.Add;
                editCheck.Checked = currentUser.Edit;
                deleteCheck.Checked = currentUser.Delete;
            }
            else
            {
                currentUser = new UserObj();

                userName.Text = null;
                passWord.Text = null;
                firstName.Text = null;
                lastName.Text = null;

                adminCheck.Checked = false;
                addCheck.Checked = false;
                editCheck.Checked = false;
                deleteCheck.Checked = false;
            }
        }

        private void clearUsers_Click(object sender, EventArgs e)
        {
            searchUserName.Text = null;
            searchFirstName.Text = null;
            searchLastName.Text = null;
        }

        private void adminResults_Sorted(object sender, EventArgs e)
        {
            String column = adminResults.SortedColumn.Name.ToString().Trim();
            String order = adminResults.SortOrder.ToString().Trim();

            MessageBox.Show(column);
            MessageBox.Show(order);

            /*switch (column)
            {
                case "First Name":
                    if (order.Equals("Ascending"))
                        dataTable.Select(null, "FIRST_NAME ASC");
                    //dataTable.DefaultView.Sort = "FIRST_NAME ASC";
                    else if (order.Equals("Descending"))
                        dataTable.Select(null, "FIRST_NAME DESC");
                    
                        //dataTable.DefaultView.Sort = "FIRST_NAME DESC";
                    break;
                case "Last Name":
                    dataTable.DefaultView.Sort = "[LAST_NAME]";
                    break;
                case "Username":
                    dataTable.DefaultView.Sort = "[USER_NAME]";
                    break;
                case "Password":
                    dataTable.DefaultView.Sort = "[PASSWORD]";
                    break;
            }*/

            switch (column)
            {
                case "First Name" :
                    if (order.Equals("Ascending"))
                        dataView.Sort = "FIRST_NAME ASC";
                    else if (order.Equals("Descending"))
                        dataView.Sort = "FIRST_NAME DESC";
                    break;
                case "Last Name" :
                    if (order.Equals("Ascending"))
                        dataView.Sort = "LAST_NAME ASC";
                    else if (order.Equals("Descending"))
                        dataView.Sort = "LAST_NAME DESC";
                    break;
                case "Username" :
                    if (order.Equals("Ascending"))
                        dataView.Sort = "USER_NAME ASC";
                    else if (order.Equals("Descending"))
                        dataView.Sort = "USER_NAME DESC";
                    break;
                case "Password" :
                    if (order.Equals("Ascending"))
                        dataView.Sort = "PASSWORD ASC";
                    else if (order.Equals("Descending"))
                        dataView.Sort = "PASSWORD DESC";
                    dataTable.DefaultView.Sort = "[PASSWORD]";
                    break;
            }
        }
    }
}


Michael


-- modified at 1:23 Friday 11th August, 2006
AnswerRe: Data Display dilemma Pin
led mike10-Aug-06 9:11
led mike10-Aug-06 9:11 
GeneralRe: Data Display dilemma Pin
sykiemikey10-Aug-06 17:41
sykiemikey10-Aug-06 17:41 
GeneralRe: Data Display dilemma Pin
led mike11-Aug-06 6:26
led mike11-Aug-06 6:26 
GeneralRe: Data Display dilemma Pin
sykiemikey11-Aug-06 13:32
sykiemikey11-Aug-06 13:32 
GeneralSolution Pin
sykiemikey12-Aug-06 1:18
sykiemikey12-Aug-06 1:18 
QuestionQuestion about DataGridView event sequence.... Pin
LongRange.Shooter10-Aug-06 8:14
LongRange.Shooter10-Aug-06 8:14 
AnswerRe: Question about DataGridView event sequence.... Pin
led mike10-Aug-06 9:07
led mike10-Aug-06 9:07 
QuestionNeed Help with Database Update using DataAdaptor Pin
Jethro6310-Aug-06 5:31
Jethro6310-Aug-06 5:31 
AnswerRe: Need Help with Database Update using DataAdaptor Pin
Ennis Ray Lynch, Jr.10-Aug-06 5:52
Ennis Ray Lynch, Jr.10-Aug-06 5:52 
GeneralRe: Need Help with Database Update using DataAdaptor Pin
Jethro6310-Aug-06 5:58
Jethro6310-Aug-06 5:58 
GeneralRe: Need Help with Database Update using DataAdaptor Pin
Ennis Ray Lynch, Jr.10-Aug-06 6:00
Ennis Ray Lynch, Jr.10-Aug-06 6:00 
GeneralRe: Need Help with Database Update using DataAdaptor Pin
Jethro6310-Aug-06 7:31
Jethro6310-Aug-06 7:31 
GeneralRe: Need Help with Database Update using DataAdaptor Pin
Ennis Ray Lynch, Jr.10-Aug-06 8:19
Ennis Ray Lynch, Jr.10-Aug-06 8:19 
AnswerRe: Need Help with Database Update using DataAdaptor Pin
bahaa_sa510-Aug-06 5:59
bahaa_sa510-Aug-06 5:59 
GeneralRe: Need Help with Database Update using DataAdaptor Pin
Jethro6310-Aug-06 7:24
Jethro6310-Aug-06 7:24 
GeneralRe: Need Help with Database Update using DataAdaptor Pin
bahaa_sa512-Aug-06 21:34
bahaa_sa512-Aug-06 21:34 
Questionbuilding cotrols for web Pin
Naveed Kamboh10-Aug-06 5:26
Naveed Kamboh10-Aug-06 5:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.