自定义功能无法正常工作

时间:2014-08-01 15:59:40

标签: c# visual-studio-2010 visual-studio function datagridview

我正在尝试对我的一些代码进行mdoify。由于它有太多if else语句,我需要使用自定义清理功能清理每件事。但问题是它给我一些错误。任何人都可以帮我解决这个问题。

我写的代码

private void btnDownload_Click(object sender, EventArgs e)
    {
        //Throw error if attachment cell is not selected.
        //make sure user select only single cell
        //and the cell have a value in it
        if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 1 && cncInfoDataGridView.SelectedCells[0].Value != null)
        {
            DownloadAttachment(cncInfoDataGridView.SelectedCells[0]);
        }
        else if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 2 && cncInfoDataGridView.SelectedCells[0].Value != null)
        {
            DownloadAttachment(cncInfoDataGridView.SelectedCells[0]);
        }
        else if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 3 && cncInfoDataGridView.SelectedCells[0].Value != null)
        {
            DownloadAttachment(cncInfoDataGridView.SelectedCells[0]);
        }
        else
            MessageBox.Show("Select a single cell from Attachment column", "Error uploading file", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

  private void UploadAttachment(DataGridViewCell dgvCell)
    {
        if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 1)
        {
            using (OpenFileDialog fileDialog = new OpenFileDialog())
            {
                //Set File dialog properties
                fileDialog.CheckFileExists = true;
                fileDialog.CheckPathExists = true;
                fileDialog.Filter = "All Files|*.*";
                fileDialog.Title = "Select a file";
                fileDialog.Multiselect = false;

                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    string strfilename = fileDialog.FileName;
                    cncInfoDataGridView.Rows[dgvCell.RowIndex].Cells[1].Value = strfilename;

                }
            }
        }
        else if (cncInfoDataGridView.SelectedCells.Count == 1 && cncInfoDataGridView.SelectedCells[0].ColumnIndex == 2)
        {
            using (OpenFileDialog fileDialog = new OpenFileDialog())
            {
                //Set File dialog properties
                fileDialog.CheckFileExists = true;
                fileDialog.CheckPathExists = true;
                fileDialog.Filter = "All Files|*.*";
                fileDialog.Title = "Select a file";
                fileDialog.Multiselect = false;

                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    string strfilename = fileDialog.FileName;
                    cncInfoDataGridView.Rows[dgvCell.RowIndex].Cells[1].Value = strfilename;
                }
            }
        }
        else
        {
            using (OpenFileDialog fileDialog = new OpenFileDialog())
            {
                //Set File dialog properties
                fileDialog.CheckFileExists = true;
                fileDialog.CheckPathExists = true;
                fileDialog.Filter = "All Files|*.*";
                fileDialog.Title = "Select a file";
                fileDialog.Multiselect = false;

                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    string strfilename = fileDialog.FileName;
                    cncInfoDataGridView.Rows[dgvCell.RowIndex].Cells[1].Value = strfilename;
                }
            }
        }
    }

清理功能代码如下,我正在尝试并提供错误

错误返回 ----->> SelectedNonNullCellFromColumn(int)'返回void,返回关键字后面不能跟一个 对象表达

SelectedSingleNonNullCell()&& SelectedCellColumn ------>>两者在当前上下文中都不存在 任何想法给我一个全新的清理代码。或者对此代码的任何帮助。

   private void btnUpload_Click(object sender, EventArgs e)
    {
        if (SelectedSingleNonNullCell() && SelectedCellColumn() > 3)
        {
            DownloadAttachment(cncInfoDataGridView.SelectedCells[0]);
        }
    }

   public void SelectedNonNullCellFromColumn(int columnId)
{
    try
  {
    return cncInfoDataGridView.SelectedCells.Count == 1 &&    - - ->> Error in this line 
       cncInfoDataGridView.SelectedCells[0].ColumnIndex == columnId
       && cncInfoDataGridView.SelectedCells[0].Value != null;

  }
    catch
  {
  }
}

0 个答案:

没有答案