I have a requirement to merge 2 column values - It's working good. But, there is no space between 2 values. Below is my code. Please let me know where i have to correct:
DataGridViewTextBoxColumn dgvctime = new DataGridViewTextBoxColumn();
dgvctime.Name = cmbColumn1.Text;
dgvctime.HeaderText = cmbColumn1.Text;
dataGridView1.Columns.Add(dgvctime);
//dataGridView1.Rows.Add(dgvctime);
int rowadded = 0;
for (int RowCount = 1; RowCount <= strfile.Length - 1; RowCount++)
{
if (strfile[RowCount].ToString() != "")
{
if (RowCount != 0)
{
//dataGridView1.Rows.Add();
string[] column = strfile[RowCount].Split('รพ');
rowadded = 0;
for (int i = 1; i < column.Length - 1; i++)
{
//dataGridView1.Rows.Add();
if ((cmbColumn1.SelectedIndex == ((i - 1) / 2)) || (cmbColumn2.SelectedIndex == ((i - 1) / 2)))
{
if (rowadded == 0)
{
rowadded = 1;
dataGridView1.Rows.Add();
}
if (column[i].ToString() != "\u0014")
{
//dataGridView1.Rows[RowCount - 1].Cells[cmbColumn1.Text].Value += column[i].ToString();
dataGridView1.Rows[RowCount - 1].Cells[cmbColumn1.Text].Value += column[i].ToString();
` }
}
}
}
}
Change this line ...
dataGridView1.Rows[RowCount - 1].Cells[cmbColumn1.Text].Value += " " + column[i].ToString();
Just add a short update
dataGridView1.Rows[RowCount - 1].Cells[cmbColumn1.Text].Value += " " + column[i].ToString();
Related
I am doing xml coloring for attributes, element, value, comment through code.
For this, I am using nested for loops which degrading the performance for large xml files because with this changes, it is checking character by character and due to this it is taking much time to recognize the target area to add coloring.
Here is the code snippet which I written for recognizing xml attributes, values, e.t.c for coloring.
protected override void OnPropertyChanged(string propertyName)
{
#region MyRegion
var pageTextLines = PageText.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
for (int i = 0; i < pageTextLines.Length; i++)
{
int startIndex = -1;
int endIndex = -1;
var thisLine = pageTextLines[i];
for (int j = 0; j < thisLine.Length; j++)
{
bool isComment = false;
bool isValue = false;
if (thisLine[j] == '<')
startIndex = j;
if (thisLine[j] == '>')
endIndex = j;
// Check if we have something like <!-- to flag that we have a comment
if (thisLine[j] == '-')
{
if (j - 2 >= 0 && j + 1 < pageTextLines.Length)
{
if (thisLine[j - 2] == '<' && thisLine[j - 1] == '!' && thisLine[j + 1] == '-')
{
j = j + 1;
startIndex = j - 2;
isComment = true;
}
}
// Check if we have something like --> to see if we're closing a comment
// or if we're at the end
if (j + 2 < thisLine.Length)
{
if (thisLine[j] == '-')
{
if (thisLine[j + 1] == '-' && thisLine[j + 2] == '>')
{
isComment = true;
j += 2;
endIndex = j;
}
}
}
}
if (startIndex != -1 && endIndex != -1)
{
if (isComment)
{
// Coloring the xml comment.
ProcessDataSelection(SelectionLayer.CommentColor,
new DataSelection(new Point(startIndex + 1, i + 1), new Point(endIndex + 1, i + 1), PositionFormat.Characters, true));
startIndex = endIndex = -1;
}
else
{
// Coloring the xml node.
ProcessDataSelection(SelectionLayer.NodeColor,
new DataSelection(new Point(startIndex + 1, i + 1), new Point(endIndex + 1, i + 1), PositionFormat.Characters, true));
startIndex = endIndex = -1;
for (int k = 0; k < thisLine.Length; k++)
{
if (thisLine[k] == ' ')
startIndex = k;
if (thisLine[k] == '=')
endIndex = k;
if (startIndex != -1 && endIndex != -1)
{
// Coloring the xml attribute.
ProcessDataSelection(SelectionLayer.AttributeColor,
new DataSelection(new Point(startIndex + 1, i + 1), new Point(endIndex + 1, i + 1), PositionFormat.Characters, true));
startIndex = endIndex = -1;
}
}
for (int l = 0; l < thisLine.Length; l++)
{
if (thisLine[l] == '>' && l < thisLine.Length - 1)
{
startIndex = l + 1;
isValue = true;
}
if (thisLine[l] == '<' && thisLine[l + 1] == '/')
{
endIndex = l - 1;
isValue = true;
}
if (startIndex != -1 && endIndex != -1)
{
if (isValue)
// Coloring the xml value.
ProcessDataSelection(SelectionLayer.ValueColor,
new DataSelection(new Point(startIndex + 1, i + 1), new Point(endIndex + 1, i + 1), PositionFormat.Characters, true));
startIndex = endIndex = -1;
}
}
}
startIndex = endIndex = -1;
}
}
#endregion
}
Can anyone please suggest me a better way to do this or better way to improve these loops ?
I am working on an ASP application which is about creating time table (scheduling of classes) for my final semester project. I have used the logic from a basic console application program of graph colouring and incorporated into my project. Though the logic for a console application works as required, I am unable to do the same for my asp project.
try
{
int n = 10, i, j, max = 0, col, ct = 0, rt = 0, m, count = 2, g = 0;
int[][] a = new int[20][];
int[] c = new int[20];
int[] arr = new int[20];
for (int k = 0; k < 20; k++)
{
c[k] = 0;
arr[k] = 0;
a[k] = new int[20];
for (int l = 0; l < 20; l++)
{
a[k][l] = 0;
}
}
string num = null, num1 = "";
string display = "SELECT * FROM tbl_Subjects WHERE USN IN(SELECT USN FROM tbl_Student WHERE Semester='" + DropDownList1.Text + "')";
con.Open();
SqlCommand cmdSql = new SqlCommand(display, con);
SqlDataReader rs;
rs = cmdSql.ExecuteReader();
while (rs.Read())
{
//count1++;
for (int k = 1; k <= n; k++)
{
g = k - 1;
num = rs[k].ToString();
arr[g] = Convert.ToInt32(num);
num1 += arr[g] + " ";
}
num1 += "\n";
for (int k = 0; k < n; k++)
{
for (int l = (k + 1); l < n; l++)
{
if (arr[k] == 1 && arr[l] == 1)
{
a[k][l] = 1;
//a[l][k] = 1;
}
}
}
}
rs.Close();
TextBox3.Text = num1;
num1 = "";
for (int k = 0; k < n; k++)
{
for (int l = 0; l < n; l++)
{
num1 += a[k][l] + " ";
}
num1 += "\n";
}
for (i = 0; i < n; i++)
{
c[i] = 0;
//for(j=0;j<n;j++)
//scanf("%d",&a[i][j]);
}
c[0] = 1; c[1] = 2;
for (i = 2; i < n; i++)
{
for (j = 0; j < n; j++)
if (a[i][j] > 0)
{
m = 0;
for (col = 0; col < n; col++)
{ if (a[i][col] > 0)rt++; if (a[col][i] > 0)ct++; }
m = rt; if (ct > rt) m = ct;
if (m < 2) { if (a[0][i] > 0)c[i] = 2; else c[i] = 1; } else { c[i] = count; if (m > max) { max = m; count++; } }
rt = 0; ct = 0;
} if (c[i] < 1) if (c[i - 1] > 1) c[i] = 1; else c[i] = 2;
}
string result = "";
for (i = 0; i < n; i++)
{
result += "Subject[" + (i + 1) + "] = " + c[i] + "\n";
if (c[i]==1)
{
TextBox4.Text += "Subject 1 = 8:15 to 9:15 \n";
}
if (c[i] == 2)
{
TextBox4.Text += "Subject 2 = 9:15 to 10:15 \n";
}
if (c[i] == 3)
{
TextBox4.Text += "Subject 3 = 10:45 to 11:45 \n";
}
if (c[i] == 4)
{
TextBox4.Text += "Subject 4 = 11:45 to 12:45 \n";
}
if (c[i] == 5)
{
TextBox4.Text += "Subject 5 = 1:30 to 2:30 \n";
}
if (c[i] == 6)
{
TextBox4.Text += "Subject 6 = 2:30 to 3:30 \n";
}
if (c[i] == 7)
{
TextBox4.Text += "Lab 1 = 8-15 to 10:15 \n";
}
if (c[i] == 8)
{
TextBox4.Text += "Lab 2 = 10:45 to 12:45 \n";
}
if (c[i] == 9)
{
TextBox4.Text += "Lab 3 = 1:30 to 3:30 \n";
}
if (c[i] == 10)
{
TextBox4.Text += "Lab 4 = 8-15 to 9:15 \n";
}
}
con.Close();
TextBox2.Text = result;
}
catch (Exception ex)
{
Label2.Text = ex.Message;
}
In the above code, I'm reading the data i.e 0's and 1's of the adjacency matrix from the database onto which the graph colouring algorithm is applied. Once the colouring has been performed, results are printed on a textbox for time being.
The problem I am facing is, the colors are not being assigned and not able to generate the time table.
//export header
for (i = 1; i <= this.datagridview1.Columns.Count; i++)
{
ExcelSheet.Cells[3, i] = this.datagridview1.Columns[i - 1].HeaderText;
}
//export data
for (i = 1; i <= this.datagridview1.RowCount; i++)
{
for (j = 1; j <= datagridview1.Columns.Count; j++)
{
ExcelSheet.Cells[i + 3, j] = datagridview1.Rows[i - 1].Cells[j - 1].Value;
}
}
This is exporting all data from datagridview but I don't want to export "Date " column and "checkbox" column
Showing like this
You can use an additional counter for columns to get the proper output (not just empty columns). Also, you can move the logics of "skipping" columns to a function.
int columnsCount = 0;
//export header
for (i = 1; i <= this.datagridview1.Columns.Count; i++)
{
if (SkipColumn(this.datagridview1.Columns[i - 1]))
continue;
columnsCount++;
ExcelSheet.Cells[3, columnsCount] = this.datagridview1.Columns[columnsCount - 1].HeaderText;
}
//export data
for (i = 1; i <= this.datagridview1.RowCount; i++)
{
columnsCount = 0;
for (j = 1; j <= datagridview1.Columns.Count; j++)
{
if (SkipColumn(this.datagridview1.Columns[i - 1]))
continue;
columnsCount++;
ExcelSheet.Cells[i + 3, columnsCount] = datagridview1.Rows[i - 1].Cells[columnsCount - 1].Value;
}
}
// ...
private bool SkipColumn(DataGridViewColumn column)
{
return column.GetType() == typeof(DataGridViewCheckBoxColumn)
|| column.Name == "Date";
}
You can add any logics to SkipColumn. Add any validations, conditions there using DataGridViewColumn properties.
Read about DataGridViewColumn properties at MSDN.
So I've got an array of integer. I want to use a loop and exclude integers that makes the equation true. So that would be like
for (int n = 0; n < first9char.Length; n++ ) {
if (first9char[n] == clickValue) {
first9char[n] = first9char[n + 1];
But then it only changes the value that is equal to not changing whole array. So is there any way to do this?
I want to use it in this loop.
if (UserSquareMethod.clickedBoxes[0] == -1) {
MessageBox.Show("You have not selected any box");
} else {
int i = 0;
do {
if (textButtonArray[clickedBox[i]].Text == "" || clickValue == "") {
textButtonArray[clickedBox[i]].Text = clickValue;
textButtonArray[clickedBox[i]].Font = new Font(textButtonArray[clickedBox[i]].Font.FontFamily, 14, FontStyle.Bold);
}
else
{
textButtonArray[clickedBox[i]].Text += "," + clickValue;
textButtonArray[clickedBox[i]].Font = new Font(textButtonArray[clickedBox[i]].Font.FontFamily, 5, FontStyle.Regular);
string[] first9char = textButtonArray[clickedBox[i]].Text.Split(new string[] { "," }, StringSplitOptions.None);
for (int j = 1; j < first9char.Length; j++)
{
for (int k = j - 1; k >= 0; k--)
{
if (first9char[j] == first9char[k])
{
if (clearNumberClicked == true)
{
first9char = Array.FindAll(first9char, x => x != clickValue);
label2.Text = first9char[0];
//int n = 0;
//for (int p = 0; p < first9char.Length; p++)
//{
// if (first9char[p] != clickValue)
// {
// first9char[n] = first9char[p];
// n++;
// label2.Text += "," + first9char[n];
// }
// }
//for (int n = 0; n < first9char.Length; n++ ) {
//if (first9char[n] == clickValue) {
// first9char[n] = first9char[n + 1];
// for ( int p = 0; p < n; p++) {
//}
//}
//}
MessageBox.Show("Clear the number" + first9char[(first9char.Length - 1)] + "and " + clickValue + " " + first9char.Length);
}
else {
first9char[j] = "";
textButtonArray[clickedBox[i]].Text = first9char[0];
MessageBox.Show("You cannot enter the same number again!"+ first9char[j]+j);
for (int m = 1; m < (first9char.Length - 1); m++) {
textButtonArray[clickedBox[i]].Text += ","+ first9char[m];
}
}
}
}
}
if (textButtonArray[clickedBox[i]].Text.Length > 9)
{
textButtonArray[clickedBox[i]].Text = first9char[0] + "," + first9char[1] + "," + first9char[2] + "," + first9char[3] + "," + first9char[4];
MessageBox.Show("You cannot enter more than 5 numbers, please clear the box if you want to enter different number." + textButtonArray[clickedBox[i]].Text.Length);
}
}
i++;
}
while (clickedBox[i] != -1);
}
}
I would use LINQ for this:
first9char = first9char.Where(x => x != clickValue).ToArray();
It just means "pick the items that don't match". If you can't use LINQ for some reason, then just keep another counter, and make sure to only loop to n from there on in:
int n = 0;
for(int i = 0; i < first9char.Length; i++) {
if(first9char[i] != clickValue) {
first9char[n] = first9char[i];
n++;
}
}
Clean and efficient.
Hi all I need some help,
I am making a type of "Game of life" game. Anyway I want to check using grid view, if the current cell's neighbor is same value as my current cell. If it is, I want to check for empty space (around the current cell) and add the neighbor's value but with and extra char.
Here is my code:
public void check()
{
Textual txt = new Textual();
for (int x = 0; x < dataGridView1.Rows.Count; x++)
{
for (int y = 0; y < dataGridView1.Rows[x].Cells.Count; y++)
{
if (dataGridView1.Rows[x].Cells[y].Value == "VS" ||
dataGridView1.Rows[x].Cells[y].Value == "SL" ||
dataGridView1.Rows[x].Cells[y].Value == "KG" ||
dataGridView1.Rows[x].Cells[y].Value == "KN")
{
if (y - 1 >= 0 &&
y + 1 < dataGridView1.ColumnCount &&
x - 1 >= 0 && x + 1 < dataGridView1.RowCount &&
y + 1 < dataGridView1.ColumnCount &&
x + 1 < dataGridView1.RowCount)
{
string value = dataGridView1.Rows[x].Cells[y].Value.ToString();
int j;
int k;
switch (value)
{
case "VS":
if (dataGridView1.Rows[x].Cells[y + 1].Value == "")
{
dataGridView1.Rows[x].Cells[y + 1].Value = "VS++";
j = x;
k = y + 1;
int[,] temp= new int[j, k];
or.oznaka = "VS";
popis.TryAdd(temp, or);
dataGridView1.Rows[x].Cells[y + 1].Style.BackColor = System.Drawing.Color.Red;
}
else if (dataGridView1.Rows[x - 1].Cells[y].Value == "")
{
dataGridView1.Rows[x - 1].Cells[y].Value = "VS++";
j = x - 1;
k = y;
int[,] temp= new int[j, k];
or.oznaka = "VS";
popis.TryAdd(temp, or);
dataGridView1.Rows[x - 1].Cells[y].Style.BackColor = System.Drawing.Color.Yellow;
}
else if (dataGridView1.Rows[x + 1].Cells[y].Value == "")
{
dataGridView1.Rows[x + 1].Cells[y].Value = "VS++";
j = x + 1;
k = y;
int[,] temp= new int[j, k];
or.oznaka = "VS";
popis.TryAdd(temp, or);
dataGridView1.Rows[x + 1].Cells[y].Style.BackColor = System.Drawing.Color.Gold;
}
else if (dataGridView1.Rows[x + 1].Cells[y + 1].Value == "")
{
dataGridView1.Rows[x + 1].Cells[y + 1].Value = "VS+";
j = x - 1;
k = y + 1;
int[,] temp= new int[j, k];
or.oznaka = "VS";
popis.TryAdd(temp, or);
dataGridView1.Rows[x + 1].Cells[y + 1].Style.BackColor = System.Drawing.Color.Green;
}
else if (dataGridView1.Rows[x - 1].Cells[y - 1].Value == "")
{
dataGridView1.Rows[x - 1].Cells[y - 1].Value = "VS+";
j = x - 1;
k = y - 1;
int[,] temp= new int[j, k];
or.oznaka = "VS";
popis.TryAdd(temp,or);
dataGridView1.Rows[x - 1].Cells[y - 1].Style.BackColor = System.Drawing.Color.Purple;
}
else if (dataGridView1.Rows[x - 1].Cells[y + 1].Value == "")
{
dataGridView1.Rows[x - 1].Cells[y + 1].Value = "VS+";
j = x - 1;
k = y + 1;
int[,] temp= new int[j, k];
or.oznaka = "VS";
popis.TryAdd(temp, or);
dataGridView1.Rows[x - 1].Cells[y + 1].Style.BackColor = System.Drawing.Color.HotPink;
}
else if (dataGridView1.Rows[x].Cells[y - 1].Value == "")
{
dataGridView1.Rows[x].Cells[y - 1].Value = "VS+";
j = x;
k = y - 1;
int[,] temp= new int[j, k];
or.oznaka = "VS";
popis.TryAdd(temp, or);
dataGridView1.Rows[x].Cells[y - 1].Style.BackColor = System.Drawing.Color.Lavender;
}
else if (dataGridView1.Rows[x + 1].Cells[y - 1].Value == "")
{
dataGridView1.Rows[x + 1].Cells[y - 1].Value = "VS+";
j = x + 1;
k = y - 1;
int[,] temp= new int[j, k];
or.oznaka = "VS";
popis.TryAdd(temp, or);
dataGridView1.Rows[x + 1].Cells[y - 1].Style.BackColor = System.Drawing.Color.LightBlue;
}
break;
}
}
}
}
}
}
Here is a function you might use to find matching neighbours. It will still be very similar if you opt for two-dimensional array instead of grid, which would be the best solution as controls should be used for interaction with humans and data should be manipulated regardless of presentation.
To find matching value given a cell (mind x and y - you switched usual meaning of them):
cell = FindCell(new Point(x, y), dataGridView1.Rows[y].Cells[x]);
And to find empty cell:
cell = FindCell(new Point(x, y), string.Empty);
EDIT: better usage example
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.Value != null && cell.Value.ToString() != string.Empty)
{
DataGridViewCell neighbour = FindCell(new Point(cell.ColumnIndex, row.Index), cell.Value.ToString());
// Found
if (neighbour != null)
{
DataGridViewCell emptyCell = FindCell(new Point(cell.ColumnIndex, row.Index), string.Empty);
if (emptyCell != null)
{
emptyCell.Value = "WHATEVERYOUREQUIREITTOBE";
}
}
}
}
}
EDIT 2: I assumed from your code your cells will have a non-null value. As it is not so, replace each cell.Value.ToString() with (cell.Value ?? string.Empty).ToString(), as I did in example.
Test for null after to make sure you have found a cell. Here is the function:
/// <summary>
/// List of locations around given location. Add to previous value to get next location.
/// </summary>
Point[] neighbours = new Point[]
{
new Point (-1, -1),
new Point (1, 0),
new Point (1, 0),
new Point (-2, 1),
new Point (2, 0),
new Point (-2, 1),
new Point (1, 0),
new Point (1, 0),
};
/// <summary>
/// Finds a cell containing given string value.
/// </summary>
/// <param name="location">Point of search</param>
/// <param name="value">Value to find</param>
/// <returns>Cell containing given value</returns>
DataGridViewCell FindCell(Point location, string value)
{
for (int i = 0; i < neighbours.Length; ++i)
{
// Move location to new point
location.Offset(neighbours[i]);
// Check boundaries
if (location.Y >= 0 && location.Y < dataGridView1.RowCount
&& location.X >= 0 && location.X < dataGridView1.Columns.Count)
{
// Get cell
DataGridViewCell cell = dataGridView1.Rows[location.Y].Cells[location.X];
// If value matches
if ((cell.Value ?? string.Empty).ToString() == value)
{
return cell;
}
}
}
// No match
return null;
}