locked
Trying to get value of data grid view selected column RRS feed

  • Question

  • User-501297529 posted

    When I select a  row in my data grid view I want to get the value of the first column. I'm setting it equal to a variable usr. I get a 'Cannot complicity convert' error. Here is a portion of the code. If you need more code I can provide that.

    usr = dgv.SelectedColumns[0];
                
    showLegend = loadedReports.Where(f.form.SUPERAPPROVED == 1 && usr.REQ_INSP_APPROVAL == 1).Count() > 0

    Monday, December 3, 2018 9:47 PM

Answers

  • User-501297529 posted

    Made a few changes to the code,  but I always get a '0' value for REQ_INSP_APPROVAL field regardless. When I select row from the data grid with a person that has a value of 1 for that field and it still shows as 0 when I debug thru the code on usr variable, this shouldn't happen I should get a value of 1

     W_USER usr = new W_USER
            {
                Name = Convert.ToString(dgvMaster.CurrentRow.Cells   [dgvMaster.CurrentCell.ColumnIndex].Value)
            };   
           
            showLegend = loadedReports.Where(f => f.form.EXEMPTFROMIP == 0 && f.form.FORMHEADER.INSPAPPROVALPROCESS == 1 && f.form.SUPERAPPROVED == 1 && usr.REQ_INSP_APPROVAL == 1).Count() > 0;I updated the code to this: 
    
    if (dgvMaster.CurrentRow == null) 
        return;
    
    object o = dgvMaster.CurrentRow.Cells[dgvMaster.CurrentCell.ColumnIndex].Value;
    
    // Error on the following line
    W_USER usr = Convert.ToString(dgvMaster.CurrentRow.Cells[dgvMaster.CurrentCell.ColumnIndex].Value);
    showLegend = loadedReports.Where(f => f.form.EXEMPTFROMIP == 0 &&
                                          f.form.FORMHEADER.INSPAPPROVALPROCESS == 1 &&
                                          f.form.SUPERAPPROVED == 1 &&
                                          usr.REQ_INSP_APPROVAL == 1)
                              .Count() > 0; 

    I noticed that the REQ_INSP_APPROVAL in another file called JonesDesigner.cs, with this code below I was getting a NULL value for REQ_INSP_APPROVAL when debugging thru the code above. The code below is what is in the JonesDesigner.cs file:

      [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_REQ_INSP_APPROVAL", DbType="Int")]
        public System.Nullable<int> REQ_INSP_APPROVAL
        {
            get
            {
                return this._REQ_INSP_APPROVAL;
            }
            set
            {
                if ((this._REQ_INSP_APPROVAL != value))
                {
                    this.OnREQ_INSP_APPROVALChanging(value);
                    this.SendPropertyChanging();
                    this._REQ_INSP_APPROVAL = value;
                    this.SendPropertyChanged("REQ_INSP_APPROVAL");
                    this.OnREQ_INSP_APPROVALChanged();
                }
            }
        }

    I updated to this:

       [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_REQ_INSP_APPROVAL", DbType="Int")]
        public int REQ_INSP_APPROVAL
        {
            get
            {
                return this._REQ_INSP_APPROVAL;
            }
            set
            {
                if ((this._REQ_INSP_APPROVAL != value))
                {
                    this.OnREQ_INSP_APPROVALChanging(value);
                    this.SendPropertyChanging();
                    this._REQ_INSP_APPROVAL = value;
                    this.SendPropertyChanged("REQ_INSP_APPROVAL");
                    this.OnREQ_INSP_APPROVALChanged();
                }
            }
        }

    I don't know if  I needed to do this but when I updated the value is always 0 as a stated initially in this post. 

    What do I need to do so I can get the proper value for REQ_INSP_APPROVAL based on what person I select in the data grid

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 5, 2018 12:43 AM

All replies

  • User-271186128 posted

    Hi bootzilla,

    When I select a  row in my data grid view I want to get the value of the first column. I'm setting it equal to a variable usr. I get a 'Cannot complicity convert' error. Here is a portion of the code. If you need more code I can provide that.

    usr = dgv.SelectedColumns[0];
                
    showLegend = loadedReports.Where(f.form.SUPERAPPROVED == 1 && usr.REQ_INSP_APPROVAL == 1).Count() > 0

    From your description, it seems that your application is a windows form application, and using the DataGridView control, right?

    If that is the case, I suggest you could refer to the following code to get the selected row values. 

    private void dgvDetails_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        txtId.Text = dgvDetails.Rows[e.RowIndex].Cells[0].Value.ToString();
        txtName.Text = dgvDetails.Rows[e.RowIndex].Cells[1].Value.ToString();
        txtCountry.Text = dgvDetails.Rows[e.RowIndex].Cells[2].Value.ToString();
        DataRow dr = dt.Rows[e.RowIndex];
        DataRow newRow = dt.NewRow();
        newRow.ItemArray = dr.ItemArray;
        dt.Rows.Remove(dr);
        dt.Rows.InsertAt(newRow, 0);
        foreach (DataGridViewRow item in dgvDetails.Rows)
        {
            dgvDetails.Rows[item.Index].Selected = false;
        }
        dgvDetails.Rows[0].Selected = true;
    }

    More details, please check these threads:

    Display DataGridView selected row cell values in TextBox in Windows Forms (WinForms) Application using C# .Net

    How to get the selected row value in datagridview control?

    As for the "Cannot complicity convert" error, please check the data type. 

    Since, this forum is discussing the Asp.net Web forms developer questions, if you have any further questions about the windows forms application, I suggest you could post your question to windows forms forum.

    Best regards,
    Dillion

    Tuesday, December 4, 2018 5:44 AM
  • User-501297529 posted

    Zhi Lv - MSFT

    Hi bootzilla,

    bootzilla

    When I select a  row in my data grid view I want to get the value of the first column. I'm setting it equal to a variable usr. I get a 'Cannot complicity convert' error. Here is a portion of the code. If you need more code I can provide that.

    usr = dgv.SelectedColumns[0];
                
    showLegend = loadedReports.Where(f.form.SUPERAPPROVED == 1 && usr.REQ_INSP_APPROVAL == 1).Count() > 0

    From your description, it seems that your application is a windows form application, and using the DataGridView control, right?

    If that is the case, I suggest you could refer to the following code to get the selected row values. 

    private void dgvDetails_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        txtId.Text = dgvDetails.Rows[e.RowIndex].Cells[0].Value.ToString();
        txtName.Text = dgvDetails.Rows[e.RowIndex].Cells[1].Value.ToString();
        txtCountry.Text = dgvDetails.Rows[e.RowIndex].Cells[2].Value.ToString();
        DataRow dr = dt.Rows[e.RowIndex];
        DataRow newRow = dt.NewRow();
        newRow.ItemArray = dr.ItemArray;
        dt.Rows.Remove(dr);
        dt.Rows.InsertAt(newRow, 0);
        foreach (DataGridViewRow item in dgvDetails.Rows)
        {
            dgvDetails.Rows[item.Index].Selected = false;
        }
        dgvDetails.Rows[0].Selected = true;
    }

    More details, please check these threads:

    Display DataGridView selected row cell values in TextBox in Windows Forms (WinForms) Application using C# .Net

    How to get the selected row value in datagridview control?

    As for the "Cannot complicity convert" error, please check the data type. 

    Since, this forum is discussing the Asp.net Web forms developer questions, if you have any further questions about the windows forms application, I suggest you could post your question to windows forms forum.

    Best regards,
    Dillion

    Hi ,

    I'm not sure if this is what I need in regards to get the value for my usr variable. Also what are the textboxes for that you have set equal the data grid columns? Do I need that? I'm not sure if it is necessary.  The variable usr is coming from a table called W_USER. so I want to I call the usr variable in the LINQ statement. So when I click on the row in the datagrid it gets that column with the name in it and then that variable will be able to pick up the usr.REQ_INSP_APPROVAL value from the person selected in the grid. So if the  person in the grid that I select is 'Smith, John'. I want to be able to get the REQ_INSP_APPROVAL value for 'Smith, John'. Does that make sense?

    Tuesday, December 4, 2018 3:43 PM
  • User-501297529 posted

    Made a few changes to the code,  but I always get a '0' value for REQ_INSP_APPROVAL field regardless. When I select row from the data grid with a person that has a value of 1 for that field and it still shows as 0 when I debug thru the code on usr variable, this shouldn't happen I should get a value of 1

     W_USER usr = new W_USER
            {
                Name = Convert.ToString(dgvMaster.CurrentRow.Cells   [dgvMaster.CurrentCell.ColumnIndex].Value)
            };   
           
            showLegend = loadedReports.Where(f => f.form.EXEMPTFROMIP == 0 && f.form.FORMHEADER.INSPAPPROVALPROCESS == 1 && f.form.SUPERAPPROVED == 1 && usr.REQ_INSP_APPROVAL == 1).Count() > 0;I updated the code to this: 
    
    if (dgvMaster.CurrentRow == null) 
        return;
    
    object o = dgvMaster.CurrentRow.Cells[dgvMaster.CurrentCell.ColumnIndex].Value;
    
    // Error on the following line
    W_USER usr = Convert.ToString(dgvMaster.CurrentRow.Cells[dgvMaster.CurrentCell.ColumnIndex].Value);
    showLegend = loadedReports.Where(f => f.form.EXEMPTFROMIP == 0 &&
                                          f.form.FORMHEADER.INSPAPPROVALPROCESS == 1 &&
                                          f.form.SUPERAPPROVED == 1 &&
                                          usr.REQ_INSP_APPROVAL == 1)
                              .Count() > 0; 

    I noticed that the REQ_INSP_APPROVAL in another file called JonesDesigner.cs, with this code below I was getting a NULL value for REQ_INSP_APPROVAL when debugging thru the code above. The code below is what is in the JonesDesigner.cs file:

      [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_REQ_INSP_APPROVAL", DbType="Int")]
        public System.Nullable<int> REQ_INSP_APPROVAL
        {
            get
            {
                return this._REQ_INSP_APPROVAL;
            }
            set
            {
                if ((this._REQ_INSP_APPROVAL != value))
                {
                    this.OnREQ_INSP_APPROVALChanging(value);
                    this.SendPropertyChanging();
                    this._REQ_INSP_APPROVAL = value;
                    this.SendPropertyChanged("REQ_INSP_APPROVAL");
                    this.OnREQ_INSP_APPROVALChanged();
                }
            }
        }

    I updated to this:

       [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_REQ_INSP_APPROVAL", DbType="Int")]
        public int REQ_INSP_APPROVAL
        {
            get
            {
                return this._REQ_INSP_APPROVAL;
            }
            set
            {
                if ((this._REQ_INSP_APPROVAL != value))
                {
                    this.OnREQ_INSP_APPROVALChanging(value);
                    this.SendPropertyChanging();
                    this._REQ_INSP_APPROVAL = value;
                    this.SendPropertyChanged("REQ_INSP_APPROVAL");
                    this.OnREQ_INSP_APPROVALChanged();
                }
            }
        }

    I don't know if  I needed to do this but when I updated the value is always 0 as a stated initially in this post. 

    What do I need to do so I can get the proper value for REQ_INSP_APPROVAL based on what person I select in the data grid

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 5, 2018 12:43 AM
  • User-501297529 posted

    Anyone with any helpful ideas on overall what I've posted so far. I would appreciate it.

    Wednesday, December 5, 2018 5:25 AM
  • User-271186128 posted

    Hi bootzilla,

    W_USER usr = Convert.ToString(dgvMaster.CurrentRow.Cells[dgvMaster.CurrentCell.ColumnIndex].Value);

    Please check above code, you are trying to convert the string to an object type. So, it will cause the "Cannot complicity convert" error.

    Then, you could set break point to debug your code and check the value. 

    Best regards,
    Dillion

    Wednesday, December 5, 2018 9:04 AM
  • User-501297529 posted

    Zhi Lv - MSFT

    Hi bootzilla,

    bootzilla

    W_USER usr = Convert.ToString(dgvMaster.CurrentRow.Cells[dgvMaster.CurrentCell.ColumnIndex].Value);

    Please check above code, you are trying to convert the string to an object type. So, it will cause the "Cannot complicity convert" error.

    Then, you could set break point to debug your code and check the value. 

    Best regards,
    Dillion

    Well that's what I'm asking assistance for is what I need to change to fix this code. I don't know I need assistance in what I can do to fix the code below. How do I fix this so I don't get the error?

    W_USER usr = Convert.ToString(dgvMaster.CurrentRow.Cells[dgvMaster.CurrentCell.ColumnIndex].Value);

    This is what I need help with:

    Made a few changes to the code,  but I always get a '0' value for REQ_INSP_APPROVAL field regardless. When I select row from the data grid with a person that has a value of 1 for that field and it still shows as 0 when I debug thru the code on usr variable, this shouldn't happen I should get a value of 1

     W_USER usr = new W_USER
            {
                Name = Convert.ToString(dgvMaster.CurrentRow.Cells   [dgvMaster.CurrentCell.ColumnIndex].Value)
            };   
           
            showLegend = loadedReports.Where(f => f.form.EXEMPTFROMIP == 0 && f.form.FORMHEADER.INSPAPPROVALPROCESS == 1 && f.form.SUPERAPPROVED == 1 && usr.REQ_INSP_APPROVAL == 1).Count() > 0;I updated the code to this: 
    
    if (dgvMaster.CurrentRow == null) 
        return;
    
    object o = dgvMaster.CurrentRow.Cells[dgvMaster.CurrentCell.ColumnIndex].Value;
    
    // Error on the following line
    W_USER usr = Convert.ToString(dgvMaster.CurrentRow.Cells[dgvMaster.CurrentCell.ColumnIndex].Value);
    showLegend = loadedReports.Where(f => f.form.EXEMPTFROMIP == 0 &&
                                          f.form.FORMHEADER.INSPAPPROVALPROCESS == 1 &&
                                          f.form.SUPERAPPROVED == 1 &&
                                          usr.REQ_INSP_APPROVAL == 1)
                              .Count() > 0; 

    I noticed that the REQ_INSP_APPROVAL in another file called JonesDesigner.cs, with this code below I was getting a NULL value for REQ_INSP_APPROVAL when debugging thru the code above. The code below is what is in the JonesDesigner.cs file:

      [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_REQ_INSP_APPROVAL", DbType="Int")]
        public System.Nullable<int> REQ_INSP_APPROVAL
        {
            get
            {
                return this._REQ_INSP_APPROVAL;
            }
            set
            {
                if ((this._REQ_INSP_APPROVAL != value))
                {
                    this.OnREQ_INSP_APPROVALChanging(value);
                    this.SendPropertyChanging();
                    this._REQ_INSP_APPROVAL = value;
                    this.SendPropertyChanged("REQ_INSP_APPROVAL");
                    this.OnREQ_INSP_APPROVALChanged();
                }
            }
        }

    I updated to this:

       [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_REQ_INSP_APPROVAL", DbType="Int")]
        public int REQ_INSP_APPROVAL
        {
            get
            {
                return this._REQ_INSP_APPROVAL;
            }
            set
            {
                if ((this._REQ_INSP_APPROVAL != value))
                {
                    this.OnREQ_INSP_APPROVALChanging(value);
                    this.SendPropertyChanging();
                    this._REQ_INSP_APPROVAL = value;
                    this.SendPropertyChanged("REQ_INSP_APPROVAL");
                    this.OnREQ_INSP_APPROVALChanged();
                }
            }
        }

    I don't know if  I needed to do this but when I updated the value is always 0 as a stated initially in this post. 

    What do I need to do so I can get the proper value for REQ_INSP_APPROVAL based on what person I select in the data grid

    Wednesday, December 5, 2018 3:39 PM
  • User-271186128 posted

    Hi bootzilla,

    Well that's what I'm asking assistance for is what I need to change to fix this code. I don't know I need assistance in what I can do to fix the code below. How do I fix this so I don't get the error?

    W_USER usr = Convert.ToString(dgvMaster.CurrentRow.Cells[dgvMaster.CurrentCell.ColumnIndex].Value);

    From your previous code, we can see the W_USER is an object, if you want to set value for the object property, you could use the following code:

     W_USER usr = new W_USER
            {
    //set value to the name property. Name = Convert.ToString(dgvMaster.CurrentRow.Cells [dgvMaster.CurrentCell.ColumnIndex].Value) };

    or

     W_USER usr = new W_USER();
     //set value to the Name property.
     usr.Name = Convert.ToString(dgvMaster.CurrentRow.Cells[dgvMaster.CurrentCell.ColumnIndex].Value);
           

    Besides, from your code, it seems that you just set the usr's name property, not set the REQ_INSP_APPROVAL property. So, when you execute the following LINQ statement, the count will be 0.

    showLegend = loadedReports.Where(f => f.form.EXEMPTFROMIP == 0 &&
                                          f.form.FORMHEADER.INSPAPPROVALPROCESS == 1 &&
                                          f.form.SUPERAPPROVED == 1 &&
                                          usr.REQ_INSP_APPROVAL == 1)
                              .Count() > 0; 

    You could check your code and set the REQ_INSP_APPROVAL value.

    And whether the usr and loadedReport contains relationship? if not, I think there is no need to check whether the usr.REQ_INSP_APPROVAL in the where clause. Try to modify your code as below:

    showLegend = loadedReports.Where(f => f.form.EXEMPTFROMIP == 0 &&
                                          f.form.FORMHEADER.INSPAPPROVALPROCESS == 1 &&
                                          f.form.SUPERAPPROVED == 1)
                              .Count() > 0; 

    Finally, it seems that you want to load related data using EF, if that is the case, might be you need to use the include method, please refer to the following articles:

    https://docs.microsoft.com/en-us/ef/core/querying/related-data

    https://docs.microsoft.com/en-us/ef/ef6/querying/related-data

    Best regards,
    Dillion

    Thursday, December 6, 2018 8:58 AM