[英]How can I add text from textbox.Text to datagridview.CurrentCell when datagridview is filtered C#?
I'm not really good at coding and after doing countless research, I cannot figure this one out. 我不太擅长编码,经过无数研究,我无法弄清楚这一点。
Basically what I want to achieve is to update a specific cell using a textBox. 基本上,我要实现的是使用textBox更新特定的单元格。 I wrote the following code based on other postings (NOTE:The DataBinding was done using the VS wizard):
我根据其他发布内容编写了以下代码(注意:DataBinding是使用VS向导完成的):
int i;
private void textBox_TextChanged(object sender, EventArgs e)
{
i = dataGridView1.CurrentCell.RowIndex;
}
private void button1_Click(object sender, EventArgs e)
{
myDatabaseDataSet.Tables[0].Rows[i][5] = textBox.Text;
}
The problem with the above code is that, when I filter dataGridView1 with this code: 上面的代码的问题是,当我使用以下代码过滤dataGridView1时:
private void comboName_SelectedIndexChanged(object sender, EventArgs e)
{
DataTableCollection tables = myDatabaseDataSet.Tables;
DataView view1 = new DataView(tables[0]);
BindingSource source1 = new BindingSource();
source1.DataSource = view1;
dataGridView1.DataSource = source1;
source1.Filter = "Name='" + comboName.Text.Replace("'", "''") + "'";
}
The code will update the dataGridView1.CurrentCell.RowIndex as expected. 该代码将按预期更新dataGridView1.CurrentCell.RowIndex。 The big problem here is that the code under button1_Click does not see the filtering, hence it would write -for example- in row[1] of the unfiltered table rather than in row[1] of the filtered table.
这里最大的问题是button1_Click下的代码看不到过滤,因此,它将(例如)写入未过滤表的row [1]而不是已过滤表的row [1]。
Any help would be greatly appreciated. 任何帮助将不胜感激。
I don't quite understand your question clearly 我不太清楚你的问题
Do you mean that the filter doesn't apply when you changed the cell text? 您是说更改单元格文本时,该过滤器不适用吗?
Try adding this to the button click event 尝试将其添加到按钮单击事件中
(myDatabaseDataSet.DataSource as DataTable).DefaultView.RowFilter = "Name='" + comboName.Text.Replace("'", "''") + "'";
Base on your code, you create a copy of the original table. 根据您的代码,创建原始表的副本。 Instead of updating the filtered table, you update the original table
myDatabaseDataSet.Tables[0].Rows[i][5] = textBox.Text;
您不必更新已过滤的表,而是更新原始表
myDatabaseDataSet.Tables[0].Rows[i][5] = textBox.Text;
. 。
The fix is obvious, I will give you some idea here... 解决方法很明显,我会在这里给您一些想法...
private void button1_Click(object sender, EventArgs e)
{
myDatabaseDataSet.Tables[0].Rows[i][5] = textBox.Text; // You need to modify this to know which is the correct row to update.
// Update the filtered table every time you update the main table.
DataTableCollection tables = myDatabaseDataSet.Tables;
DataView view1 = new DataView(tables[0]);
BindingSource source1 = new BindingSource();
source1.DataSource = view1;
dataGridView1.DataSource = source1;
source1.Filter = "Name='" + comboName.Text.Replace("'", "''") + "'";
}
Good Luck. 祝好运。
first off, you're gonna have to find a unique column that is unique for every row. 首先,您将必须找到每行唯一的唯一列。 then you can select the dataRow of your dataset based on that like this :
那么您可以像这样选择数据集的dataRow:
int uniqueIDFromDatagrid = (int)dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[uniqueIdentifierColumn.Index].Value;
DataRow targetRow = myDataSet.Tables[0].Rows.OfType<DataRow>().First(x => (int)x["uniqueIdentifierColumn"]== uniqueIDFromDatagrid);
and then you can update the source on the right row and column like this : 然后您可以像这样在右侧的行和列上更新源:
targetRow[5] = textBox1.Text;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.