在c#.net中的datagridview单元格中追加文本文件项目 [英] Append textfile items in datagridview cell in c#.net

查看:50
本文介绍了在c#.net中的datagridview单元格中追加文本文件项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有文本文件abc.txt,我想点击datagridview单元格时,txt文件项应该在datagridview单元格的建议中显示。

当我从代码中读取项目时,代码工作正常,但是当我从文本文件中读取项目时,它无法正常工作。

以下是我的代码:



我尝试过:



I have text file abc.txt and I want when click on datagridview cell the txt file items should show in suggestion in datagridview cell.
The code is working fine when i read the items from code, but when i read items from text file it is not working.
Below is my code :

What I have tried:

//Reading from code (working)

 public AutoCompleteStringCollection autocompleteApp()
        {
            AutoCompleteStringCollection str = new AutoCompleteStringCollection();
            str.Add("Yes");
            str.Add("No");
            return str;
        }

// Reading from text file (Not working)       
 public AutoCompleteStringCollection autocompleteApp()
        {  
            AutoCompleteStringCollection str = new AutoCompleteStringCollection();
            int count = 0;
            string line;
            StreamReader file = new StreamReader(@"C:\abc.txt");
            while ((line = file.ReadLine()) != null)
            {
                CLBAssign.Items.Add(line);
                count++;
            } 
            return str;
        }

// When click on datagridviewcell
private void gridIncident_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
            int column = datagridview1.CurrentCell.ColumnIndex;
            string headerText = datagridview1.Columns[column].HeaderText;
            TextBox tb = e.Control as TextBox;
            if (headerText.Equals("App Name"))
            {
                if (tb != null)
                {
                    tb.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                    tb.AutoCompleteCustomSource = autocompleteApp();
                    tb.AutoCompleteSource = AutoCompleteSource.CustomSource;
                }
            }
}

推荐答案

您尚未为str分配任何值以下函数中的变量

You haven't assigned any value to "str" variable in below function
public AutoCompleteStringCollection autocompleteApp()
        {  
            AutoCompleteStringCollection str = new AutoCompleteStringCollection();
            int count = 0;
            string line;
            StreamReader file = new StreamReader(@"C:\abc.txt");
            while ((line = file.ReadLine()) != null)
            {
                CLBAssign.Items.Add(line);
                count++;
            } 
            return str;
        }



添加类似


add something like

str.Add(line)


这篇关于在c#.net中的datagridview单元格中追加文本文件项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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