Image not shown in the Grid for the new row

Unanswered Image not shown in the Grid for the new row

  • Monday, September 03, 2012 9:47 AM
     
      Has Code

    Hi I have a Grid where i am not binding any data to that Grid It is editable. I have set the property AllowUserToAddRows = true; In the load i am writing the below code

    DataGridViewTextBoxColumn txtPhoneNr = new DataGridViewTextBoxColumn(); txtPhoneNr.Name = "phone"; txtPhoneNr.MaxInputLength = 60; txtPhoneNr.Width = 150; txtPhoneNr.AutoSizeMode = DataGridViewAutoSizeColumnMode.None; txtPhoneNr.HeaderText = Phonebook.FrmNumbers_FreeNumbersGrid_NumberColumn; DataGridViewTextBoxColumn txtName = new DataGridViewTextBoxColumn(); txtName.Name = "Name"; txtName.MaxInputLength = 100; txtName.Width = 150; txtName.AutoSizeMode = DataGridViewAutoSizeColumnMode.None; txtName.HeaderText = Phonebook.FrmNumbers_FreeNumbersGrid_NameColumn; DataGridViewImageColumn deleteColumn = new DataGridViewImageColumn(); deleteColumn.Name = "Delete"; deleteColumn.Image = Properties.Resources.Deleteimage; deleteColumn.ValuesAreIcons = false; deleteColumn.Width = 30; deleteColumn.HeaderText = ""; dataGrid.Columns.Add(txtName); dataGrid.Columns.Add(txtPhoneNr); dataGrid.Columns.Add(deleteColumn); dataGrid.ColumnHeadersVisible = true; dataGrid.AllowUserToAddRows = true;

    When I run the code, the grid  is shown as below it is not displaying the image.

    As the image is not showing i have added the event

     void dataGrid_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
            {
                e.Row.Cells["Delete"].Value = Properties.Resources.Deleteimage;
            }
    The Problem now is When i click on the row the image is displayed but in the new row which is added automatically, image is not displayed. Could You please suggest me how to display the image for the new row.

All Replies

  • Tuesday, September 04, 2012 3:17 AM
    Moderator
     
     

    Hi Raju_Hai,

    You can try to use DataGridView.DefaultValuesNeeded event. The default value will be set when you add new rows. It is explained in this thread:

    http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/4f638278-124e-43e8-bdfb-fcd3d667cd2b

    You can have a look at it.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • Tuesday, September 04, 2012 5:23 AM
     
     

    Hi Chester Hong,

    I used the same solution as mentioned in the link only. 

    http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/4f638278-124e-43e8-bdfb-fcd3d667cd2b, but the problem i am faciing is when we click on the new row then the image is showing. By default when we load ,the image is not displayed.

  • Tuesday, September 04, 2012 6:17 AM
    Moderator
     
      Has Code

    Hi Raju_Hai,

    Maybe you can try to also use RowsAdded event to set the image when user add rows.

            private void dataGrid_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
            {
                if (dataGrid.ColumnCount == 3)
                {
                    for (int i = e.RowIndex; i < e.RowIndex + e.RowCount; ++i)
                    {
                        dataGrid.Rows[i].Cells["Delete"].Value = Properties.Resources.Deleteimage;
                    }
                }
            }
    

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • Tuesday, September 04, 2012 6:34 AM
     
     

    Hi Chester Hong,

    I have tried to add the dataGrid_RowsAdded it works fine, when we try to write something on the Grid a new row is added and the image is displayed  in the new row .

    But by default when the grid is loaded the image is not displayed. I need to display the image by default when the grid is loaded. And i obeserved that dataGrid_RowsAdded  event is not fired when the grid is loaded first time. If i try to enter the name then the dataGrid_RowsAdded is fired.

  • Wednesday, September 05, 2012 8:28 AM
    Moderator
     
      Has Code

    Hi Raju_Hai,

    It seems that the image is not set because the row is added when you start to add column. And the column number is 1, not 3. The event is not fired any more when you add the other 2 columns.

    I find one workaround to add the collection of columns using AddRange, rather than add columns one by one:

    //dataGrid.Columns.Add(txtName);
    //dataGrid.Columns.Add(txtPhoneNr);
    //dataGrid.Columns.Add(deleteColumn);
    dataGrid.Columns.AddRange(txtName, txtPhoneNr, deleteColumn);
    Best regards,

    Chester Hong
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • Wednesday, September 05, 2012 12:02 PM
     
     

    Hi Chester Hong,

    I tried by adding the columns using the Addrange then also the image is not displayed default.

    Now my requirement  has changed ,i will make image column visible as false when we are creating columns. I need to make the image column visible=true, only when i enter some text in the editable row. How can i acheive this functinality. When i enter something new row gets added for that image column should be visible=false; but for the image column which i entered should be visible =true;

  • Wednesday, September 12, 2012 9:00 AM
    Moderator
     
      Has Code

    Hi Raju_Hai,

    Sorry for late response.

    I mean you can use both dataGrid.Columns.AddRange() and RowsAdded event. That works for me.

    I'm not sure whether I understand your new requirement. You can directly set the Visible property of the DataGridViewColumn like:

    dataGrid.Columns["Delete"].Visible = false;
    Best regards,

    Chester Hong
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • Thursday, September 13, 2012 10:28 AM
     
     

    Hi Chester Hong,

    Every thing worked fine when placed dataGrid_RowsAdded,dataGrid_DefaultValuesNeeded by default we are able to see the image and when new row gets added also image is getting displayed.

    But now another issue occured if we select the new row added and then select the already added row then the image which is in the new row is not displayed. Could you suggest me to resolve this issue.