-2
private void moviesGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    MovieDetailsForm form = new MovieDetailsForm(MovieDetailsForm.MovieViewMode.Read);

    if (e.ColumnIndex==5)
    {
         form.ShowDialog();
    }
}

I am trying to view the details of a movie when I press the view details button in the datagridview but for some reason I can't get it to work. The place of the buttons in the datagridview is 5.

I'd show a ss but unfortunately I cant, yet.

TGnat
  • 3,903
  • 8
  • 35
  • 46
user2057185
  • 39
  • 2
  • 7
  • Please explain the meaning of `i cant get it to work`. Are you getting any exception??? – Microsoft DN Aug 20 '13 at 13:42
  • `i cant get it to work` Can you be A LOT more specific and detailed about what the problem actually is? – tnw Aug 20 '13 at 13:42
  • I am not getting any errors, simply nothing happens at all, i want it to show the movieDetailsform when i press a button in the datagridview. while i store the value of the first cell into a tag. – user2057185 Aug 20 '13 at 13:45
  • 1
    Instead of comparing to a fixed value (i.e. 5), why not compare to the `.ColumnIndex` property of the DataGridViewColumn you're interested in? That way it won't matter if you decide to rearrange/add/remove columns later. – Bridge Aug 20 '13 at 13:47
  • 2
    Also, why not set a break point, press the button column and check the value of `e.ColumnIndex`? – Bridge Aug 20 '13 at 13:48

3 Answers3

2

The place of the buttons in the datagridview is 5

It means that the column is the fifth column?

If yes, don't forget that index in .Net are generally zero-based index. So it would be:

if (e.ColumnIndex==4)

Also, good remark from KyleMit, don't create an instance of MovieDetailsForm if you don't use it:

if (e.ColumnIndex==4)
{
     MovieDetailsForm form = new MovieDetailsForm(MovieDetailsForm.MovieViewMode.Read);
     form.ShowDialog();
} 
Chris
  • 8,527
  • 10
  • 34
  • 51
1

Just to summarize what others have said and to help out your coding style...

private void moviesGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
  if (dataGridView1.Columns["colDetailButton"].DisplayIndex == e.ColumnIndex)
  {
    // my guess is you also need other data, like the movie's IMDB number
    string imdbValue = dataGridView1.Rows[e.RowIndex].Cells["colImdbValue"].Value.ToString();
    using (var form = new MovieDetailsForm(MovieDetailsForm.MovieViewMode.Read))
    {
      form.ImdbValue = imdbValue;
      form.ShowDialog();
    }
  }
  else
  {
    // Remove this debugging code once you get your code working
    Console.WriteLine("ColumnIndex {0} was clicked." e.ColumnIndex);
  }
}
0

See this answer as to how to How to handle click event in Button Column in Datagridview? for a good overview of what to do. So long as you only have a single button, you actually don't have to specify the column index at all, which makes your code less fragile to change. Although, Chris is right, that indexes are zero based so you'd need a ColumnIndex of 4 to get the 5th column. You also don't have to new up your form unless you actually want to show it, so I'd move the declaration into the if statement like this:

private void moviesGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    //make sure click not on header and column is type of ButtonColumn
    if (e.RowIndex >= 0 && ((DataGridView)sender).Columns[e.ColumnIndex].GetType() ==  _
                           typeof(DataGridViewButtonColumn))
    {
         MovieDetailsForm form = new MovieDetailsForm(MovieDetailsForm.MovieViewMode.Read);
         form.ShowDialog();
    }
}
Community
  • 1
  • 1
KyleMit
  • 30,350
  • 66
  • 462
  • 664