为什么我的dataGridView1_SelectionChanged方法仅在该方法内的一个控件中触发,为什么它在触发前列出第一行项目?

时间:2019-07-16 05:11:40

标签: c#-4.0 datagridview selectionchanged

当我选择一行时,在datagridview中,使用箭头键没有任何反应。当我使用鼠标选择一行时,textBox1会暂时显示最初选择的项目,然后切换到该textBox中正确输入的下一行值,但是什么也没有发生。在添加排序代码之前,箭头键选择和鼠标选择都可以正常工作。从那以后,我摆脱了排序代码,但是该程序无法像以前那样运行。不,我没有备份,因为我是菜鸟,还需要再学习一次“机会”,哈哈。

在我尝试向我的datagridview标头列中添加排序代码的那一刻,它停止了工作。我认为此问题与Current Cell属性有关,但我不知道如何解决该问题。

私有无效dataGridView1_SelectionChanged(对象发送者,EventArgs e) {

int currentRow = dataGridView1.CurrentRow.Index;
richTextBox1.Text = dataGridView1[30, CurrentRow].Value.ToString();

//clear all previous checks in checklistbox

foreach (int i in checkedListBox1.CheckedIndices)
{
   checkedListBox1.SetItemCheckState(i, CheckState.Unchecked);
}


// int currentRow = dataGridView1.CurrentCell.RowIndex;
//check the checkboxes from the datagridview data
if (dataGridView1[20, CurrentRow].Value.ToString() == "1")
{
   checkedListBox1.SetItemCheckState(0, CheckState.Checked);
}
else if (dataGridView1[20, CurrentRow].Value.ToString() == "0")
{
   checkedListBox1.SetItemCheckState(0, CheckState.Unchecked);
}


if (dataGridView1[21, CurrentRow].Value.ToString() == "1")
{
   checkedListBox1.SetItemCheckState(1, CheckState.Checked);
}
else if (dataGridView1[21, CurrentRow].Value.ToString() == "0")
{
   checkedListBox1.SetItemCheckState(1, CheckState.Unchecked);
}


if (dataGridView1[22, CurrentRow].Value.ToString() == "1")
{
   checkedListBox1.SetItemCheckState(2, CheckState.Checked);
}
else if (dataGridView1[22, CurrentRow].Value.ToString() == "0")
{
   checkedListBox1.SetItemCheckState(2, CheckState.Unchecked);
}
//a bunch more of the above all checking datagridview and 
//checking/unchecking checklist boxes.....

//set the progress bar with the amount of checked boxes
progressBar1.Maximum = 100;
progressBar1.Style = System.Windows.Forms.ProgressBarStyle.Blocks;

switch (checkedListBox1.CheckedItems.Count)
{
   case 0:
       progressBar1.Value = 0;
       label12.Text = "Status: 0%";
       break;
   case 1:
       progressBar1.Value = 10;
       progressBar1.ForeColor = Color.Red;
       label12.Text = "Status: 10%";
       break;
   case 2:
       progressBar1.Value = 20;
       progressBar1.ForeColor = Color.OrangeRed;
       label12.Text = "Status: 20%";
       break;             
   //a bunch of these going up to 100%
}

//load the initially selected row into the textBoxs

if (dataGridView1[0, CurrentRow].Value.ToString() != string.Empty)
{
   this.textBox1.Text = (string)dataGridView1[0, CurrentRow].Value;
}
else if (dataGridView1[0, CurrentRow].Value.ToString() == string.Empty)
{
   this.textBox1.Text = "";
}

if (dataGridView1[1, CurrentRow].Value.ToString() != string.Empty)
{
   this.textBox2.Text = (string)dataGridView1[1, CurrentRow].Value;
}
else if (dataGridView1[1, CurrentRow].Value.ToString() == string.Empty)
{
   this.textBox2.Text = "";
}

//a bunch of these all pulling data from the datagridview and putting 
//into textboxes

//load richtextbox data from employer specific text file

string filePath = @"\\path redacted because its work 
related\Data\redacted\redacted\redacted\File Consultants\Brandon\Notes\";

string fileName = textBox1.Text + "_" + textBox2.Text + "_Notes.txt";

if (File.Exists(filePath + fileName))
{
   richTextBox1.Text = File.ReadAllText(filePath + fileName);                
}
else
{
   DialogResult result = MessageBox.Show("Notes file does not exist. 
   Would you like to create one?", "Create Notes?", 
   MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);


   if (result == DialogResult.Yes)
   {
       string textToSave = richTextBox1.Text;      

       MessageBox.Show(filePath + fileName);

       SaveAndWriteToTextFile save = new SaveAndWriteToTextFile();
       save.SaveAndWriteToTextFileMethod(textToSave, filePath + 
       fileName);
   }
   else if (result == DialogResult.No)
   {

   }
}

}

当我选择一个datagridview行时,它应该更新所有文本框,清单框和一个richtextbox。当前,它仅更新第一个文本框,并且仅在用鼠标单击时更新,并在显示所选值之前显示最初选择的datagridview [0,0] .Tostring()值。

0 个答案:

没有答案