I haver this piece of code. which is giving me a nullreferenceexception..

private void dgvMessages_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvMessages.CurrentRow == null) 
            {
                return;
            }

            try
            {
                int select = dgvMessages.CurrentRow.Index; //NULL REFERENCE EXCEPTION

                DateTime dateTimeMessage = Convert.ToDateTime(dgvMessages.Rows[select].Cells[1].Value.ToString());

                string date = dateTimeMessage.ToLongDateString();
                string time = dateTimeMessage.ToLongTimeString();

                lblDate.Text = "Date: " + date;
                lblTime.Text = "Time: " + time;

                txtMessage.Text = dgvMessages.Rows[select].Cells[2].Value.ToString();
            }
            catch (NullReferenceException ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

Edited 5 Years Ago by xanawa: n/a

This is the even of some control. This will fire a always file on the form load, becuas the dataGridView always selects the [0,0] based sell - this is upper, left cell.
And when it does, the event fires up.
What you can do, to prevent executing the code on a form load (or when ever you do NOT want it to be fired), you can set a flab; boolean flag.
Take a look at this example:

bool bJumpEvent;
        public Form1()
        {
            InitializeComponent();
            dataGridView1.Columns.Add("col1", "Column1");
            dataGridView1.Rows.Add("a");
            dataGridView1.Rows.Add("b");
            
            //set flag to true, so it will not execute the event:
            bJumpEvent = true;
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (!bJumpEvent)
            {
                //put your code in here!
            }
            else
                bJumpEvent = false;
        }

This way you can set a flag when ever you want something MUST not be executed. Then you simple set the flag back to default state (in my case this is false), and code will be executed.
I hope you got the point of using flags.

i am sorry i made a mistake i am not calling it in the form_load.. but still it is not working.

try this code:

int index = dataGridView1.CurrentCell.RowIndex;

didnt work either.. is it maybe because i am filling the data in the grid view frm the code on form load?

I dont nkow.
Its got to be something, becase the code I just gave you works here.
Can you show me the code you use to populated dgv?

yes ofcourse

private void FillDataGridView()
        {
            ManagerNotification mgrNotification = new ManagerNotification();
            List<Notification> listOfNotifications = mgrNotification.GetAllNotificationByUserID(staffID);

            foreach (Notification n in listOfNotifications)
            {
                int num = dgvMessages.Rows.Add();

                bool read = Convert.ToBoolean(n.Read.ToString());

                //if (read)
                //{
                //    dgvMessages.Rows[num].Cells[0].Value = draw@"C:\Users\Anna Marie\Desktop\Software Project Assignment\Images\Icon\iconDecode.jpg"; //HOW TO INSERT IMAGE IN DGV
                //}
                //else
                //{
                //    dgvMessages.Rows[num].Cells[0].Value = @"C:\Users\Anna Marie\Desktop\Software Project Assignment\Images\Icon\iconEncode.jpg";
                //}
                dgvMessages.Rows[num].Cells[1].Value = n.DateTimeCreated.ToString();
                dgvMessages.Rows[num].Cells[2].Value = n.Message.ToString();
            }
        }

Hmmm, some strnage code. How do you get the row index number.
The problem for sure is lying in tis code.
Try using this one:

private void FillDataGridView()
        {
            ManagerNotification mgrNotification = new ManagerNotification();
            List<Notification> listOfNotifications = mgrNotification.GetAllNotificationByUserID(staffID);

            for(int i = 0; i < listOfNotifications.Count; i++)
            {
                 dgvMessages.Rows.Add();
                 dgvMessages.Rows[num].Cells[1].Value = listOfNotifications[i].DateTimeCreated.ToString();
                 dgvMessages.Rows[num].Cells[2].Value = listOfNotifications[i].Message.ToString();
            }
        }

And your code for getting the row index in SelectionChanged event wil work!

This article has been dead for over six months. Start a new discussion instead.