简体   繁体   English

当对datagridview进行C#过滤时,如何将textbox.Text中的文本添加到datagridview.CurrentCell中?

[英]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.

相关问题 在C#单击按钮后,如何从DataGridView等于textbox.Text中选择一行? - How can I select a row from the DataGridView equals textbox.Text after clicking a button by C# ? 如何在C#的datagridview中将文本添加到行标题中? - How can I add text to my rowheaders in a datagridview, C#? C# WPF:如何从另一个页面访问页面控件以获取 textbox.text - C# WPF : how can i access page controls from another page to get textbox.text 为什么我datagridview.currentcell不会将光标定位在所需的单元格中? - why i datagridview.currentcell wouldn't position the cursor in the desired cell? (C# - Forms) 如何从 TextBox.Text 获取用户输入? - (C# - Forms) How do i get user input from TextBox.Text? C#如何从DataGrid(不是DataGridView)提取文本? - C# How do I extract Text from a DataGrid (Not DataGridView)? 无法将文本从DataGridView拖放到TextBox C# - Unable to drag and drop text from DataGridView to TextBox C# 如何在 C# 中将多行文本从 access 数据库显示到 DataGridView - How can I display multi line text from access database into DataGridView in C# 如何从c #Windows窗体中的文本框的文本内容更新datagridview列 - How to update the column of datagridview from the text contents of textbox in c# Windows form 如何在 C# 中同时关注文本框和数据网格视图? - How can i focus on textbox and datagridview at the same time in c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM