Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question of state with DataGridView

Status
Not open for further replies.

moMoney2009

Programmer
Nov 7, 2007
7
US
Got a tricky one that seems to deal with state ... and I don't know the underlying infrastructure of C# well enough to know the answer.

so whenever a cell is clicked ... the row number is saved in a list (rowsSelected) but whenever CustomerDelete_deleteBtn_Click is called when the deleteBtn is clicked ... it will execute the callback method correctly BUT rowsSelected is empty ... what is going on here?

by the way, i have made sure that items are actually being added to the list inside of dataGridView1_CellClick. Thanks :)

Side note: Class B contains an instance of Class A, thus, the linkage.

CLASS A
Code:
//CALLBACK METHODS
        /***************************************************************/

        /* Will add the row number to rowsSelected whenever a row is clicked.
         * Will remove the row number if clicked again.*/
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            currRow = e.RowIndex;
            bool flag = true;
            int pos = 0;
            List<int>.Enumerator top = rowsSelected.GetEnumerator();
            while ((top.MoveNext()) && (flag))
            {
                if (top.Current == currRow)
                {
                    rowsSelected.RemoveAt(pos);
                    flag = false;
                }
                pos++;
            }
            if (flag)
                rowsSelected.Add(currRow);
        }

        public void CustomerDelete_deleteBtn_Click(object sender, EventArgs e)
        {
            if (rowsSelected.Count > 0)
            {
                string tempStr = "";
                Utilites.ModalDialog temp = new TheMockElectricCompany.Utilites.ModalDialog();
                temp.ShowDialog();
                if (temp.DialogResult == DialogResult.Yes)
                {
                    //string[] param = new string[7];
                    List<int>.Enumerator top = rowsSelected.GetEnumerator();
                    while (top.MoveNext())
                    {
                        int row = top.Current;
                        for (int col = 0; col < rowsSelected.Count; col++)
                            tempStr += dataGridView1[col,row].Value + "  ";
                        MessageBox.Show(tempStr);
                    }
                }
                //param[column] = (string)customerGrid[column, row].Value;
                /*string fName = (string)customerGrid[0,row].Value;
                string lName = (string)customerGrid[1, row].Value;
                string addr = (string)customerGrid[2, row].Value;
                string zip = (string)customerGrid[3, row].Value;
                string city = (string)customerGrid[4, row].Value;
                string state = (string)customerGrid[5, row].Value;
                string pNum = (string)customerGrid[6, row].Value;
                MessageBox.Show(fName + " " + lName + " " + addr + " " + zip + " " + city + " " + state + " " + pNum);*/

            }
        }

CLASS B
Code:
public partial class CustomerDelete : Form
    {
        private MainForm parentForm;
        private int row, col;
        private MECDataGridView dataGridView;

        public CustomerDelete(MainForm parentForm)
        {
            InitializeComponent();
            

            this.dataGridView = new MECDataGridView();
            this.parentForm = parentForm;
            //this.row = NOT_SELECTED;
            //this.col = NOT_SELECTED;

            InitializeDelegates();
        }

        private void InitializeDelegates()
        {
            /*Will Callback to referenced method in this class from SearchPanel 
             *class whenever searchBtn is clicked*/
            searchPanel1.AddCallbackToSearchClick(new EventHandler(searchBtn_Clicked));

            /*Will callback to MECDataGridView class where the referenced 
             *method exists.*/
            this.deleteBtn.Click += new EventHandler(this.dataGridView.CustomerDelete_deleteBtn_Click); ;
        }
 
Not done much UI in VS2005. In VS2003 got round this by:

On cell_click() store the selected row in a private int. Refer to the private member in the _delete() mthod.

Hope this helps.

Graeme

"Just beacuse you're paranoid, don't mean they're not after you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top