Datagridview column filter

dualshck012

Active member
Joined
Jan 14, 2018
Messages
29
Location
Leyte, Philippines
Programming Experience
1-3
I have a databind datagridview with 4 columns, except for the first column which is an added column in the control for a symbol/color row. The first column will be filter flag to indicate an active or inactive users(Doctors). The 2nd column is a databinded column from database which has the value of either 0 or 1 (inactive or active). If the value is 0, the 1st datagridview column will indicate a corresponding inactive image/red color row, else if the value is '1' it will indicate a corresponding active image/green color row. I have a recent C# code here, but it's doing nothing on my control.

C#:
private void dg_vw_actve_doc_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 1 && !isInit)
            {
                var valueCell = dg_vw_actve_doc.Rows[e.RowIndex].Cells[e.ColumnIndex];
                var imgCell = dg_vw_actve_doc.Rows[e.RowIndex].Cells[e.ColumnIndex + 1]; // Whatever index your colImg is
                                                                                         // char firstCharacterInCell = valueCell.Value.ToString()[1];
                                                                                         //if (firstCharacterInCell == 'A')
                if (int.Parse(valueCell.Value.ToString()) == 1)
                    imgCell.Value = Properties.Resources.active;
                else
                    imgCell.Value = Properties.Resources.inactive;
            }
        }

My Datagriview load event:
C#:
private void LOAD_ACTIVE_DOCTORS()
        {

            try
            {
                mysqlconstring.conn.Open();
                string query301 = "SELECT hprovider.chatactv, CONCAT(hpersonal.firstname, '  ', hpersonal.lastname) AS 'Doctors', accno as 'Accreditation' FROM hpersonal INNER JOIN hprovider ON  hpersonal.employeeid=hprovider.employeeid WHERE  hprovider.licno LIKE 'MD%'";
                mysqlconstring.cmd = mysqlconstring.conn.CreateCommand();
                mysqlconstring.cmd.CommandType = CommandType.Text;
                mysqlconstring.cmd.CommandText = query301;
                mysqlconstring.adapt.SelectCommand = mysqlconstring.cmd;
                DataSet dtSet = new DataSet();
                mysqlconstring.adapt.Fill(dtSet);
                dg_vw_actve_doc.DataSource = dtSet.Tables[0].DefaultView;
                // to hide unbound column
                if (dg_vw_actve_doc.Columns.Count != 0)
                {
                    dg_vw_actve_doc.Columns[1].Visible = true;
                }
}
            catch (MySqlException err01)
            {
                MessageBox.Show(err01.ToString(), "Their is an Error in the Database connection. Please contact your SH System Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw;
            }
            finally
            {
                if (mysqlconstring.conn.State == ConnectionState.Open)
                {
                    mysqlconstring.conn.Close();
                }
            }
        

        }
 
Firstly, there's no point creating a DataSet if all you need is a DataTable and there's no point binding the DefaultView because that's where the data comes from when you when you bind the DataTable. Create a DataTable, bind that to a BindingSource, which you would add in the designer, and bind that to the grid. If you need to sort or filter, use the Sort and Filter properties of the BindingSource.
 
it's all working now, but how can i manage to work the code at
'private void dg_vw_actve_doc_CellValueChanged(object sender, DataGridViewCellEventArgs e)' to a button click? It only works during editing of values in the cell. See screenshot.
 

Attachments

  • test.png
    test.png
    170.2 KB · Views: 65
That code in the the cell value changed event handler is only dependent on the the event arguments row and column index values. Furthermore, real work is only done if column 1 changes. So why not move the code in that event handler out to a helper method that take a row number? In pseudo-code:
C#:
SetImage(int row)
{
    // most of the your code to set the image from your OP
}

CellValueChangedEvent(..., e)
{
    if (e.ColumnIndex == 1 && !isInit)
        SetImage(e.RowIndex)
}

ButtonClickEvent()
{
    rowNumber = some criteria that you failed to tell us about
    SetImage(rowNumber)
}
 
Back
Top Bottom