hi there,

i have a datagridview in that i have coded the CellBeginEdit.
how can i attach the CellBeginEdit event in another function that i have implemented in C#
???


thanxxx

You can simply go into the event properties of the designer (little lightning) and map it to same method in the dropdownlist.

or add something similar to designer code view:

this.lbReports.SelectedIndexChanged += new System.EventHandler( this.lbReports_DoubleClick );
                        this.lbReports.BindingContextChanged += new System.EventHandler( this.lbReports_DoubleClick );

Judith,

You can either do it the way mentioned above if the function you have created has the same signature as the CellBeginEdit Event Handler, or, another easy way to do it would be to call the function at the end of the pre-existing event handler as shown...

private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
  //exisiting code goes here

  YourFunction();
}

You can simply go into the event properties of the designer (little lightning) and map it to same method in the dropdownlist.

or add something similar to designer code view:

this.lbReports.SelectedIndexChanged += new System.EventHandler( this.lbReports_DoubleClick );
                        this.lbReports.BindingContextChanged += new System.EventHandler( this.lbReports_DoubleClick );

hi

what is lbReports and lbReports_DoubleClick. when i add it to my code and complie it give a blue line underneath them,

why is this do i have to add any package or anything

Judith,

You can either do it the way mentioned above if the function you have created has the same signature as the CellBeginEdit Event Handler, or, another easy way to do it would be to call the function at the end of the pre-existing event handler as shown...

private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
  //exisiting code goes here

  YourFunction();
}

hey how can i do it the other way round

i need to call the event inside the function how can i do this??


thanx

If you've written a function alrready that you want to get called when the event is fired, simply place a call to the function at the end of the event.

So you won't want to call the event from the function, but rather the function from the event...

hi

what is lbReports and lbReports_DoubleClick. when i add it to my code and complie it give a blue line underneath them,

why is this do i have to add any package or anything

Well these are just examples of methods name of your C# code

If you've written a function alrready that you want to get called when the event is fired, simply place a call to the function at the end of the event.

So you won't want to call the event from the function, but rather the function from the event...

hey, below is the event with the code that i want to call in another event actually

private void dgvSubContract_Validating(object sender, CancelEventArgs e)
        {
            MessageBox.Show("dgvSubContract_Validating");
            int l = 0;
            int r = dgvSubContract.CurrentCell.RowIndex;
            l = m.getSubCount();
            if (dgvSubContract.Rows[r].Cells[0].Value != null)
            {
                for (int i = 0; i < l; i++)
                {
                    String[] SubName = new String[l];
                    SubName = m.FillSubContractName();

                    if ((dgvSubContract.Rows[r].Cells[0].Value != SubName[i]))
                    {
                        //MessageBox.Show("user must enter data");
                        dgvSubContract.Rows[r].Cells[0].Value = "";
                        dgvSubContract.Rows[r].Cells[1].Value = "";
                        dgvSubContract.Rows[r].Cells[2].Value = "";
                        dgvSubContract.Rows[r].Cells[3].Value = "";
                        dgvSubContract.Rows[r].Cells[4].Value = "";

                    } MessageBox.Show("value cleared");
                }
            }
        }

how can i trigger this event in another event

thanxxx

This should be what you're looking for...

private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
  //exisiting code goes here

  CancelEventArgs e = new CancelEventArgs();
  dgvSubContract_Validating(this, e);
}

This should be what you're looking for...

private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
  //exisiting code goes here

  CancelEventArgs e = new CancelEventArgs();
  dgvSubContract_Validating(this, e);
}

hey i have the a code for making the datagrid view combo box an editable text box in the datagrid view. and also when i select a value from the datagrid view combo box the values are entered t other other datagrid view combo boxes. and when i click on it the values get cleared. and when i try to add write data to the atagrid view combo box the value is cleared how can i avoid this,
the code is added below

private void dgvSubContract_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (dgvSubContract.CurrentCell == null)
                return;



            if ((dgvSubContract.CurrentCell.Value != null))
            {
                dgvSubContract.CommitEdit(DataGridViewDataErrorContexts.Commit);
                if (dgvSubContract.CurrentCell.ColumnIndex == 0)
                {
                    int r = dgvSubContract.CurrentCell.RowIndex;

                    String subName = dgvSubContract.Rows[r].Cells[0].Value.ToString();
                    String[] a = new String[4];

                    a = m.getSubDetails(subName);

                    dgvSubContract.Rows[r].Cells[1].Value = a[0].ToString();
                    dgvSubContract.Rows[r].Cells[2].Value = a[1].ToString();
                    dgvSubContract.Rows[r].Cells[3].Value = a[2].ToString();
                    dgvSubContract.Rows[r].Cells[4].Value = a[3].ToString();
                    dgvSubContract.Rows[r].Cells[1].ReadOnly = true;
                    dgvSubContract.Rows[r].Cells[1].ReadOnly = true;
                    dgvSubContract.Rows[r].Cells[1].ReadOnly = true;
                    dgvSubContract.Rows[r].Cells[1].ReadOnly = true;
                }

            }

        }
    
        private void dgvSubContract_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
            {
                DataGridViewComboBoxEditingControl cbo = e.Control as DataGridViewComboBoxEditingControl;
                cbo.DropDownStyle = ComboBoxStyle.DropDown;
                dgvSubContract.Validating += new CancelEventHandler(dgvSubContract_Validating);
            }
        }

        private void dgvSubContract_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            int count1 = 0;
            int r = dgvSubContract.CurrentCell.RowIndex;

            for (int a = 0; a < 4; a++)
            {
                if (dgvSubContract.Rows[r].Cells[a].Value != null)
                {
                    count1++;
                }
            }

            if (count1 == 4)
            {
                for (int c = 0; c <= 4; c++)
                {
                    dgvSubContract.Rows[r].Cells[c].Value = "";
                    dgvSubContract.Rows[r].Cells[c].ReadOnly = false;
                   
                } MessageBox.Show("value cleared");

            } 
         }
  
 private void dgvSubContract_Validating(object sender, CancelEventArgs e)
        {
            DataGridViewComboBoxEditingControl cbo = sender as DataGridViewComboBoxEditingControl;
            DataGridView grid = cbo.EditingControlDataGridView;
            object value = cbo.Text;

            if (cbo.Items.IndexOf(value) == -1)
            {

                DataGridViewComboBoxColumn cboCol = grid.Columns[grid.CurrentCell.ColumnIndex] as DataGridViewComboBoxColumn;
                // Must add to both the current combobox as well as the template, to avoid duplicate entries...
                
                cbo.Items.Add(value);
                cboCol.Items.Add(value);
                grid.CurrentCell.Value = value;
            }
            //Add value to list if not there
            if ((dgvSubContract.CurrentCell.Value != null))
            {
                dgvSubContract.CommitEdit(DataGridViewDataErrorContexts.Commit);
                if (dgvSubContract.CurrentCell.ColumnIndex == 0)
                {
                    int r = dgvSubContract.CurrentCell.RowIndex;

                    String subName = dgvSubContract.Rows[r].Cells[0].Value.ToString();
                    String[] a = new String[4];

                    a = m.getSubDetails(subName);

                    dgvSubContract.Rows[r].Cells[1].Value = a[0].ToString();
                    dgvSubContract.Rows[r].Cells[2].Value = a[1].ToString();
                    dgvSubContract.Rows[r].Cells[3].Value = a[2].ToString();
                    dgvSubContract.Rows[r].Cells[4].Value = a[3].ToString();
                    dgvSubContract.Rows[r].Cells[1].ReadOnly = true;
                    dgvSubContract.Rows[r].Cells[1].ReadOnly = true;
                    dgvSubContract.Rows[r].Cells[1].ReadOnly = true;
                    dgvSubContract.Rows[r].Cells[1].ReadOnly = true;
                }

            }
}

 private void dgvSubContract_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex = dgvSubContract.CurrentCell.RowIndex;
            int colIndex = dgvSubContract.CurrentCell.ColumnIndex;

            if (colIndex == 2)
                if (dgvSubContract.Rows[rowIndex].Cells[2].Value != null)
                {   
                    String email = dgvSubContract.Rows[rowIndex].Cells[2].Value.ToString();
                    if (m.IsValidEmailAddress(email))
                    {
                        String val = dgvSubContract.Rows[rowIndex].Cells[2].Value.ToString();
                        if (m.ValidEmialDB(val))
                        {
                            MessageBox.Show("email used enter different");
                            dgvSubContract.Rows[rowIndex].Cells[2].Value = "";
                        }
                        else
                            MessageBox.Show("valid emial");
                    }
                    else
                    {
                        MessageBox.Show("Invalid email");
                        dgvSubContract.Rows[rowIndex].Cells[2].Value = "";
                    }
                }
                else
                    MessageBox.Show("Enter email address");

        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.