0

How can I prevent from clicking or selecting RowHeaders?
I want that when a new user clicks on a row or cell, then move all information to an update Form and it's working fine.

But, if a user clicks on a RowHeader, then it should select all rows and generate an error.

I know I can hide RowHeaders, but I want to show RowHeaders and I want to prevent users from clicking on them.

I have used the SelectionMode property of the DataGridview and set it to CellSelect/FullRowSelect etc, but it did not help.

Is it possible to disable RowHeaders and not let people select RowHeaders?

Here is my code so far:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{ 
    decimal accNo = Convert.ToDecimal(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
    var item = db.UserAccount.Where(a => a.AccountNo == accNo).FirstOrDefault(); 

    txtAccountNo.Text = item.AccountNo.ToString();

    txtFirstName.Text = item.FirstName;
    txtLastName.Text = item.LastName;
    txtAddressUpNew.Text = item.Address.ToString();
 }
zx485
  • 28,498
  • 28
  • 50
  • 59
Helen Tekie
  • 515
  • 1
  • 6
  • 23
  • __Do not__ call a `DataGridView`a `GridView` or a `DataGrid` and vice versa!! This is wrong and confusing as those are different controls. Always call things by their __right__ name! – TaW Nov 09 '18 at 22:06
  • 1
    @TaW thank you, you are right. I have now edited my question. – Helen Tekie Nov 09 '18 at 22:13
  • You may want to look into the SelectionMode. Change ftom the default (RowHeader) to something else.. – TaW Nov 09 '18 at 22:21
  • @TaW thank you again, but as I mentioned in my question I have already tried to solve changing SelecttionMode but did not work for mig. – Helen Tekie Nov 09 '18 at 22:48
  • CellSelect should work. FullRowSelect or course not. – TaW Nov 09 '18 at 22:52
  • If you want the click on a rowheader to select all rows&cells code the rowheadermouseclick event to do so and, what, create an error? rather unclear specifiction, imo. – TaW Nov 09 '18 at 23:25
  • @TaW. I did. Then if I click somewhere in the rowheader then I get error like "Index was out of range. Must be non-negative and less than the size of the collection" I want to prevent clicking the header. – Helen Tekie Nov 09 '18 at 23:26
  • You should check the columnIndex before using it. For the rowheaders it will be -1. Same for RowIndex when clicking a columnheader. Don't confuse them ;-) – TaW Nov 09 '18 at 23:27
  • @TaW thank you. I'am new for c#. Can you tell me how to check and where to Place maybe in Load_Form? But how can I check? – Helen Tekie Nov 10 '18 at 00:02
  • 1
    If the code you show throws an error in the dataGridView1_CellClick, as you decribe, this is where you should test for a non-negative index. You can't really/easily prevent the user clicking where he want but you can write your code to not always react or to do different things when clicking cells or headers – TaW Nov 10 '18 at 00:05
  • 1
    @TaW Aha ... if (e.RowIndex > -1) .... OK, thank you. Now it's working – Helen Tekie Nov 10 '18 at 00:51
  • try to use `RowHeaderMouseClick` event – Muj Nov 13 '18 at 09:32

0 Answers0