管理DataGridView单元格索引 [英] Manage DataGridView cell index

查看:69
本文介绍了管理DataGridView单元格索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,
我已将数据绑定到具有11列的datagridview.用户必须填写第4列和第10列.除第4列和第11列外的所有其他列均为只读.

我在第4列中输入ID,然后自动填充第5列和第6列.在我填充第4列并单击tab后,焦点应位于第10列.在填充column10并单击Tab键后,应将第4列的下一行聚焦为

现在我的问题是在第4列中输入值并单击Tab列5后得到焦点.它应该专注于第10栏.

按Tab键后如何使第10列成为焦点?

我的代码是这样的...


I have bound the data to datagridview which has 11 columns. Column 4 and Column 10 has to be filled by user. All other columns except column 4 and 11 are read only.

I enter ID in column 4 and column 5 and 6 are filled automatically. And after i fill column 4 and click tab , focus should be column 10.Also after column10 is filled and clicking tab key next row of column 4 should be focused

Now my problem is after i enter the value in column 4 and click Tab Column 5 get foucs. It should rather focus on column 10.

How can make column 10 focus after i press Tab key?

My code is sth like this...

private void dgvBaking_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.ColumnIndex == 3 && e.RowIndex != -1)
                {
                    // Get the contID from the the SP Ref No Column
                    int contID = 0;
                    if (dgvBaking.Rows[e.RowIndex].Cells["SPRefNo"].Value.ToString() == "0")
                    {
                        return;
                    }
                    if (string.IsNullOrEmpty(dgvBaking.Rows[e.RowIndex].Cells["SPRefNo"].Value.ToString()) == false && tmp.IsInteger(dgvBaking.Rows[e.RowIndex].Cells["SPRefNo"].Value.ToString()))
                    {
                        contID = Convert.ToInt32(dgvBaking.Rows[e.RowIndex].Cells["SPRefNo"].Value);
                    }
                    Contractor objCont = new Contractor();
                    //Get contractor name and phone
                    DataTable dtCont = objCont.getContractorForCSV(contID);
                    if (dtCont.Rows.Count > 0)
                    {
                        dgvBaking.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value = dtCont.Rows[0][0].ToString() + " " + dtCont.Rows[0][1].ToString();
                        dgvBaking.Rows[e.RowIndex].Cells[e.ColumnIndex + 2].Value = dtCont.Rows[0][2].ToString();
                    }
                    else
                    {
                        dgvBaking.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "";
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                string strLog = string.Empty;
                strLog = "========================\r\n";
                strLog = strLog + System.DateTime.Now.ToString() + "\r\n";
                strLog = strLog + "========================\r\n";
                strLog = strLog + & #34;============================================================================
=========================================================\r\n";
                strLog = strLog + "Form:''" + this.Name + "''\r\nError:"+ ex.Message +"\r\n" + ex.StackTrace.ToString() + "\r\n";
                strLog = strLog + & #34;============================================================================
=========================================================\r\n";
                ErrorLog rLog = new ErrorLog();
                rLog.writeLog(strLog);
            }

        }

        private void dgvBaking_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (dgvBaking.Columns[e.ColumnIndex].Name == "SPRefNo")
            {
                if (String.IsNullOrEmpty(e.FormattedValue.ToString())==false)
                {
                    if (!tmp.IsInteger(e.FormattedValue.ToString()))
                    {
                        dgvBaking.Rows[e.RowIndex].ErrorText =
                            "Enter the correct contractor reference number";
                        e.Cancel = true;
                    }
                }
            }
        }

        private void dgvBaking_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            dgvBaking.Rows[e.RowIndex].ErrorText = String.Empty;
        }
  private void dgvBaking_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Tab)
            {
                dgvBaking.ClearSelection();
                int CurrentColumn, CurrentRow;
                CurrentColumn = dgvBaking.CurrentCell.ColumnIndex;
                CurrentRow = dgvBaking.CurrentCell.RowIndex;
                if (CurrentColumn == 9 && CurrentRow != dgvBaking.Rows.Count - 1)
                {
                    //dgvBaking.Rows[CurrentRow + 1].Cells[3].Selected = true;
                    dgvBaking.CurrentCell = dgvBaking.Row[CurrentRow+1].Cells[3];
                }
                else
                {
                    if (dgvBaking.CurrentCell.ColumnIndex == 3)
                    {
                      //  dgvBaking.Rows[CurrentRow].Cells[9].Selected = true;
                        dgvBaking.CurrentCell = dgvBaking.CurrentRow.Cells[9];
                    }
                    else
                    {
                        //dgvBaking.Rows[CurrentRow].Cells[3].Selected = true;
                        dgvBaking.CurrentCell = dgvBaking.CurrentRow.Cells[3];
                    }
                }
                e.Handled = true;
            }
            base.OnKeyDown(e);
        }




任何帮助都会被应用




Any Help will be appriciated

推荐答案

您是否已逐步执行此代码以查看其为何未达到您的期望?
Have you stepped through this code to see why it''s not doing what you expect ?


这篇关于管理DataGridView单元格索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆