I have a question about converting from dataGridView to textbox. I want when see x replace it with the Headername
I have dataGridview here:
Name A B C D ...
kiki x x ...
lola x ...
maja x x x ...
I want to have in my textbox:
kiki A,C ...
lola B ...
maja A,C,D ...
My code would look like this i think;
string abc = "";
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 1; i < dataGridView1.Columns.Count - 1; i++)
{
if(dataGridView1.Rows[i].Cells[j].Value.ToString() != "x")
abc = dataGridView1.Columns[j].HeaderText.ToString();
richTextBox1.Text += dataGridView1.Rows[i].Cells["Name"].Value.ToString() + " " + abc;
//or something like that, can some1 give me a key?
}
}
Can someone help me with it? don't know how to do it, please give me any ideas
This should work for you. this code is putting everything into one multiline richTextBoxand each row is add in a new line:
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
string text = dataGridView1.Rows[i].Cells["Name"].Value.ToString() + " " ;
bool added = false;
for (int j = 1; j < dataGridView1.Columns.Count; j++)
{
if (dataGridView1.Rows[i].Cells[j].Value.ToString() == "x")
{
text += dataGridView1.Columns[j].HeaderText.ToString() + ",";
if (!added)
added = true;
}
}
if(added)
{
text = text.Remove(text.Length - 1); //to remove ',' at the end
}
richTextBox1.Text += text + Environment.NewLine; //add to richTextbox1 with newline
}
Related
Hi all hoping for some direction. I have a text file that I am reading into a C# Form as a simple string array. I want to take each element and put them into their own text box on the form. I know I can do it as
textBox1.Text = myArray[0];
textBox2.Text = myArray[1];
and so forth but I was hoping for some sort of for or foreach statement to iterate through the array and textboxes.
For reference the form has 50 boxes on it and the array will always have 50 elements.
Thanks in advance!
Update, I appreciate both comments it got me pointed in the right direction. I was really struggling to think through it. What I ended up coming up with is:
//Read Data
string[] lines = File.ReadAllLines(Globals.savePath);
//Write Data To Form
for (int i = 1; i <= 20; i++)
{
TextBox textBox = (TextBox)groupBox1.Controls["textBox" + i];
textBox.Text = lines[i-1];
}
for (int i = 21; i <= 40; i++)
{
TextBox textBox = (TextBox)groupBox2.Controls["textBox" + i];
textBox.Text = lines[i - 1];
}
for (int i = 41; i <= 60; i++)
{
TextBox textBox = (TextBox)groupBox3.Controls["textBox" + i];
textBox.Text = lines[i - 1];
}
for (int i = 61; i <= 80; i++)
{
TextBox textBox = (TextBox)groupBox4.Controls["textBox" + i];
textBox.Text = lines[i - 1];
}
I am sure there is a cleaner way to do this. But this worked for me and was much better then doing the whole
textbox1.Text = myArray[0]
Plus it had the added benefit of translating over when to the next part where I was editing text boxes and than writing back to the text file.
string[] lines = new string [81];
for (int i = 1; i <= 20; i++)
{
TextBox textBox = (TextBox)groupBox1.Controls["textBox" + i];
lines[i - 1] = textBox.Text;
}
for (int i = 21; i <= 40; i++)
{
TextBox textBox = (TextBox)groupBox2.Controls["textBox" + i];
lines[i - 1] = textBox.Text;
}
for (int i = 41; i <= 60; i++)
{
TextBox textBox = (TextBox)groupBox3.Controls["textBox" + i];
lines[i - 1] = textBox.Text;
}
for (int i = 61; i <= 80; i++)
{
TextBox textBox = (TextBox)groupBox4.Controls["textBox" + i];
lines[i - 1] = textBox.Text;
}
lines[81] = "End";
File.Delete(Globals.savePath);
File.WriteAllLines(Globals.savePath, lines);
Once again Thank You to those that commented!
This is my first attempt at an answer, so fingers crossed this is helpful. I would store the textboxes in a list, then when you loop through your array, just do something like the following:
var maxItems = myArray.Length;
for(int loop = 0; loop < maxItems; loop++)
{
textBoxItemList[loop].Text = myArray[loop];
}
This could be tweaked to determine the loop size using the smallest array size, so you didn't go out of bounds. The maxItems is there just as an example of where you would set this up.
Hope that helps.
I'm a student and was absent for one meeting so I'm self studying for this topic.
There are 4 activities and this is the last one, but its seems that I'm stuck.
Basically what i want is to display the selected combo box item in the given input number of rows and columns. In my code, it only displays the first row and and first column.
How can I make it display a grid (row x column) of the given character (eg. 4x4, 5x2, 3x4)?
int x, y,z;
y = int.Parse(textBox1.Text);
z = int.Parse(textBox2.Text);
label4.Text = " ";
for (x = 1; x <= y; x++)
{
label4.Text = label4.Text + comboBox1.Text;
}
for (x = 1; x <= z; x++)
{
label4.Text = label4.Text + comboBox1.Text + "\n";
}
Just modify you second loop :)
string line = label4.Text;
// line = "####", now we want to append it three times to Label4 (because first line is already there)
for (x = 1; x < z; x++)
{
label4.Text += "\n" + line;
}
I took example value of lines (rows) to be 4 :) Hope I explained well. Another approach wold be to use nested loops:
for(int i = 0; i < z; i++)
{
for(int j = 0; j < y; j++)
{
label4.Text += comboBox1.Text;
}
label4.Text += "\n";
}
you better use StringBuilder
int x, y, z;
StringBuilder sb = new StringBuilder();
y = int.Parse(textBox1.Text);
z = int.Parse(textBox2.Text);
label4.Text = " ";
for (x = 1; x <= y; x++)
{
sb.Append( label4.Text + comboBox1.Text);
}
sb.Append("\n");
for (x = 1; x <= z; x++)
{
sb.Append(label4.Text + comboBox1.Text + "\n");
}
label4.Text = sb.ToString();
take a look at this:
int x = 4;
int y = 3;
string s = "#";
for (int ix = 0; ix < x; ix++)
{
label2.Text += s;
}
for (int iy = 0; iy < y; iy++)
{
label2.Text += s + "\n";
}
if you need all corner:
int y = 4;
int x = 10;
StringBuilder sb = new StringBuilder();
string s = string.Empty;
for (int i = 1; i < x + 1; i++)
{
sb.Append("#");
}
s = sb.ToString() + "\n";
//Remaining string
String rStr = string.Empty;
for (int i = 0; i < y; i++)
{
rStr += s;
}
label2.Text = rStr;
I'm coding like below but it works incorrect.It perform(plus and delete) only 2->3 rows if data has 5->6 duplicate data.
Update and It works
for (int i = 0; i < dataGridView1.RowCount - 1; i++) //compare data
{
var Row = dataGridView1.Rows[i];
string abc = Row.Cells[1].Value.ToString() + Row.Cells[2].Value.ToString().ToUpper();
// MessageBox.Show(abc);
for (int j = i + 1; j < dataGridView1.RowCount; j++)
{
var Row2 = dataGridView1.Rows[j];
string def = Row2.Cells[1].Value.ToString() + Row2.Cells[2].Value.ToString().ToUpper();
if (abc == def)
{
Row.Cells[5].Value = Convert.ToDouble(Row.Cells[5].Value.ToString()) + Convert.ToDouble(Row2.Cells[5].Value.ToString());
dataGridView1.Rows.Remove(Row2);
j--;
}
}
}
This should do the trick for you:
for (int i = 0; i < dataGridView1.RowCount - 1; i++) //compare data
{
var Row = dataGridView1.Rows[i];
string abc = Row.Cells[0].Value.ToString() + Row.Cells[1].Value.ToString().ToUpper();
for (int j = i+1; j < dataGridView1.RowCount; j++)
{
var Row2 = dataGridView1.Rows[j];
string def = Row2.Cells[0].Value.ToString() + Row2.Cells[1].Value.ToString().ToUpper();
if (abc == def)
{
Row.Cells[2].Value = (int)Row.Cells[2].Value + (int)Row2.Cells[2].Value;
dataGridView1.Rows.Remove(Row2);
j--;
}
}
}
You basically need to keep track of j variable as you remove rows from the collection.
If you're a fan of LINQ and don't mind a bit of a convoluted code, here is another approach:
for (int i = 0; i < dataGridView1.RowCount; i++) //compare data
{
var R = dataGridView1.Rows[i];
var V = R.Cells[0].Value.ToString() + R.Cells[1].Value.ToString().ToUpper();
var DupRows = dataGridView1.Rows.Cast<DataGridViewRow>().Skip(i + 1).
Where(r => r.Cells[0].Value.ToString() + r.Cells[1].Value.ToString().ToUpper() == V);
R.Cells[2].Value = (int)R.Cells[2].Value + DupRows.Sum(r => (int)r.Cells[2].Value);
foreach (var DupRow in DupRows)
DupRow.Tag = "Del";
}
for (int i = 0; i < dataGridView1.RowCount; i++)
{
var R = dataGridView1.Rows[i];
if (R.Tag?.ToString() == "Del")
{
dataGridView1.Rows.Remove(R);
i--;
}
}
As a word of advice, this kind of stuff is handled far more easily in the back-end. Whatever your DataGridView is bound to, be it a DataTable or a generic collection, you should implement duplicate removal there instead of directly playing with DataGridView cells.
I have already changed Multiline properties to true and WordWrap to false but it didnt work.
The code to show the table is :
for (int i = -1; i < membershipmat.Rows.Count; i++)
{
rtf.Append(#"{\trowd");
ColumnPosition = ColWidth;
for (int j = 0; j < membershipmat.Columns.Count; j++)
{
rtf.Append(#"\clwWidth" + ColWidth.ToString() + #"\cellx" + ColumnPosition.ToString());
ColumnPosition += ColWidth;
}
rtf.Append("{");
for (int j = 0; j < membershipmat.Columns.Count; j++)
{
if (i == -1)
rtf.Append(#"{\fs24\f3\b\intbl {\ltrch Cluster " + (j + 1).ToString() + #"}\li0\ri0\sa0\sb0\fi0\ql\sl15\slmult0\cell}");
else
rtf.Append(#"{\fs22\f3\b0\intbl {\ltrch " + membershipmat.Rows[i].ItemArray[j].ToString() + #"}\li0\ri0\sa0\sb0\fi0\ql\sl15\slmult0\cell}");
}
rtf.Append("}");
rtf.Append(#"{\trowd");
rtf.Append(#"\row}}");
}
The result looks like this :
im tunning this loop and what to populate the array with the output of my methods, im not sure about that last part "array2DB[i,i] =" how shold i do this.
updated loop based on replyes
private void BackGroundLoop()
{
for (int i = 1; i < 31; i++)
{
string txbName = "br" + i + "txt" + '3';
TextBox txtBCont1 = (TextBox)this.Controls[txbName];
string string1 = txtBCont1.Text.ToString();
UpdateFormClass.runUserQuery(string1);
array2DB[0, i - 1] = int.Parse(UpdateFormClass.gamleSaker.ToString());
array2DB[1, i - 1] = int.Parse(UpdateFormClass.nyeSaker.ToString());
}
}
I'm not 100% sure what you want to do, but you want probably this instead of your last line:
array2DB[0, i - 1] = int.Parse(UpdateFormClass.gamleSaker.ToString());
array2DB[1, i - 1] = int.Parse(UpdateFormClass.nyeSaker.ToString());
-1 in index is needed, because arrays are indexed from 0 in .NET.
This is the most you can do, without running into exception:
int[,] array2DB = new int[2, 30];
for (int i = 0; i < 30; i++)
{
string txbName = "br" + i + "txt" + '3';
TextBox txtBCont1 = (TextBox)this.Controls[txbName];
string string1 = txtBCont1.Text.ToString();
UpdateFormClass.runUserQuery(string1);
array2DB[0,i] = int.Parse(UpdateFormClass.gamleSaker.ToString());
array2DB[1,i] = int.Parse(UpdateFormClass. nyeSaker.ToString());
}
Note that you can't have array2DB[2, *] or above because it will generate an arrayoutofbound exception.
You have to use two for loops. One for each the x and y axis of the array.
for (int i = 0; i < 2; i++){
for (int j = 0; j < 30; j++)
{
....
array2DB[i,j] = int.Parse(UpdateFormClass.gamleSaker.ToString())
, int.Parse(UpdateFormClass.nyeSaker.ToString());
}
}