Reputation: 111
I have method to change cell in datagridView and works fine when i rewrite 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 .
Upvotes: 1
Views: 3721
Reputation: 1366
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.
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.
Upvotes: 0
Reputation: 4221
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.
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.
Upvotes: 1