简体   繁体   English

将datagridview中的单元格更改为空值

[英]change cell in datagridview to empty value

I have method to change cell in datagridView and works fine when i rewrite text (String) . 我有更改datagridView中的单元格的方法,当我重写text(String)时可以正常工作。
But I want for example rewrite email to empty value and I dont know how do this. 但是我想例如将电子邮件重写为空值,但我不知道该怎么做。
I can only rewrite email to another email (string to another string) 我只能将电子邮件重写为另一封电子邮件(字符串为另一字符串)

My method to change cell is : 我更改单元格的方法是:

public void ChangeCellEmail(int col, string[] emails)
    {


        string sep = ";";
        string text = "";

        foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
        {


            for (int i = 0; i < emails.Length ;i++)
            {

                if (emails[i].ToString().Trim() != "")
                {


                    text = text + emails[i] + sep ;
                    dataGridView1.Rows[cell.RowIndex].Cells[col].Value = text;
                }
            }

        }

    }

The calling code of my method is 我的方法的调用代码是

            string mail = txtBox1.Text;
            string mail1 = txtBox2.Text;
            string mail2 = txtBox3.Text;
            string mail3 = txtBox4.Text;
            string mail4 = txtBox5.Text;

            string[] mails = new string[] { mail, mail1, mail2, mail3, mail4 };


            frm1.ChangeCellEmail(2, mails);
            this.Dispose();

Thanks for help guys . 谢谢你们的帮助。

Using the following code I can pass in 5 complete email address's of which some / all could be "empty" and the tempVar will always contain the correct data. 使用以下代码,我可以传入5个完整的电子邮件地址,其中某些/全部可能为“空”,并且tempVar将始终包含正确的数据。

public Form1()
    {
        InitializeComponent();

        const string mail = "First";
        const string mail1 = "Second";
        const string mail2 = "Third";
        const string mail3 = "";
        const string mail4 = "Fifth";

        var mails = new string[] { mail, mail1, mail2, mail3, mail4 };

        ChangeCellEmail(2, mails);
    }


    public void ChangeCellEmail(int col, string[] emails)
    {
        var sep = ";";
        var text = "";
        var tempVar = ""; //New temp variable (representing your dataGrid.value)

        for (int emailList = 1; emailList < 5; emailList++)
        {
            for (var i = 0; i < emails.Length; i++)
            {

                if (emails[i].Trim() != "")
                {
                    text = text + emails[i] + sep;
                    tempVar = text;
                }
                else
                {
                    tempVar = string.Empty;
                }
            }

        }

    }

Check the tempVar on each loop and you'll see what I am referring to. 检查每个循环上的tempVar,您将看到我指的是什么。

Here's a suggested solution: 这是一个建议的解决方案:

public void ChangeCellEmail(int emailColumnIndex, string[] emails)
{
  var emailsAsCsv = string.Join(";", emails.Where(e => !string.IsNullOrWhiteSpace(e)));
  foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
  {
    dataGridView1.Rows[cell.RowIndex].Cells[emailColumnIndex].Value = emailsAsCsv;
  }
}

This updates the selected cells' Email column with the a semi-colon-separated list of non-empty emails. 这将以分号分隔的非空电子邮件更新所选单元的“ Email列。

Usage: 用法:

var emailColumnIndex = 2; // The third column in the DataGridView (zero-indexed)
var emails = new[] {txtBox1.Text, txtBox2.Text, txtBox3.Text, txtBox4.Text, txtBox5.Text};
ChangeCellEmail(emailColumnIndex, emails);

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM