I am hoping somebody can help me out here and that this questions won't be too vague.
I am trying to determine a way that I can highlight the differences between two text strings within a winform.
I have the original string in one column of a data grid and the string to compare to in a second column. What I would like is to highlight where the comparison string is different within the data grid view itself.
Example:
Thanks for the hep
Thanks for the help!!
where the l, and two ! would be highlighted
I have tried the following (Shout out to Bouam for his help in this Previous Post):
for (int i = 0; i < TextProcessingResults.RowCount; i++)
{
if (TextProcessingResults.Rows[i].Cells[1].Value != null)
{
if ((string)TextProcessingResults.Rows[i].Cells[1].Value != (string)TextProcessingResults.Rows[i].Cells[2].Value)
{
TextProcessingResults.Rows[i].DefaultCellStyle.ForeColor = Color.Red;
}
}
}
But this only highlights which rows are different not at the character level. I'm new to all of this, so is this a crazy endeavor to undertake?
Could anybody help or point me towards a resource that would be useful?
You can create a new DatGridViewColumn based on RichTextBox and then use your new rich text column, to highlight differences in rich text.
This may help you:
RichTextBox Cell in a DataGridView
And here is an ouptput based on this:
You can apply the algorithm you need to compare strings for differences to columns and using this DataGridViewRichTextBoxColumn highlight the differences.
To learn more about creating custom datagridview column types you can take a look at Build a Custom NumericUpDown Cell and Column for the DataGridView Control.
Related
I am trying to show a dropdown composed of several CheckBoxes (a list of CheckBox) in a DataGridView. Specifically, the DataGridView provide a DataGridViewComboBoxColumn that allows to have a dropdown with several items. However, if you try to add as a datasource of the column a list of checkboxes you will find out that the solution is not working (the checkboxes are not shown).
In the following link there is a solution on "regular" ComboBox: https://www.codeproject.com/Articles/31105/A-ComboBox-with-a-CheckedListBox-as-a-Dropdown
However, I need it for DataGridViewComboBoxColumn. Someone has an idea of what can be done to reach the goal? Thanks for answering (I link an example of the code below)
for (int i = 0; i < dataGridView.Rows.Count; i++)
{
// Put List of Checkboxes in DataGridViewComboBoxCell for each row of the Grid
((DataGridViewComboBoxCell) dataGridView.Rows[i].Cells[1]).DataSource = new List<CheckBox> { new CheckBox(), new CheckBox(), new CheckBox(), new CheckBox() };
}
I have modified DataGridViewColumns in past (OK, not entirely myself, according to some CodeProject cook book too :-) ), so I have some idea.
However this can't probably work in this case, there's a problem with this idea. You'd not only need to change how the cell looks, but also think how to store the data. How would you assign a separate datasource to each single DataGridViewComboboxCell? If possible, that would be probably quite extensive work, probably a class handling the structure and the events.
If I can offer a solution, you can have another object (i.e. another DataGridViw at the side with CheckBoxColumn and TextColumn which would load on DataGridView1.SelectionChanged), or you can populate it in a modal form on CellClick of that cell and feed back a list of selected items strings on close, etc. I did that in past many times and I don't think there's an easy way around it.
So I made a program that contains a lot of textboxes and comboboxes for some calculations. The program has an Output button and an Input button. At the moment I have managed the Input through skipping lines and inserting the information like this:
ComboBoxes like this.
if (File.ReadLines(filename).Skip(23).Take(1).First() == "TEMP")
{
TEMP5.SelectedIndex = 2;
}
Textboes like this.
TEMP1.Text = File.ReadLines(filename).Skip(25).Take(1).First();
and the Output is just a massive block of Output containing newlines
System.IO.File.WriteAllText(filename,bla bla bla
Is there a better way?? There has to be.
EDIT: So one button "Output" saves all data contained in textboxes and comboboxes in a txt file.
The "Input" button makes you able to choose the txt file and copy the data into the boxes.
I usually find that if someone has to name boxes "Box1", "Box2", "Box3"... "Box99" then their doing something wrong. Unless each value has a specific, unique calculation that needs to be done with it, you can usually make things cleaner with a list.
In WPF, you can use ItemsControls like DataGrid to generate rows of TextBoxs that will link back to your list with data binding. If that looks like something that might fit, I would highly recommend looking into it. It will save you a lot of work in the long run.
That being said, if you do have a series of controls named consecutively ("Box1", "Box2", "Box3", etc), you can use FindName() to access them dynamically:
var outputBuilder = new StringBuilder();
for (int i = 1; i < 100; i++)
{
ComboBox box = (ComboBox)FindName("TEST" + i.ToString());
outputBuilder.AppendLine(box.Text);
}
System.IO.File.WriteAllText(filename, outputBuilder.ToString());
i'm making a vocabulary trainer via visual C#. I'm storing words in data grid view table, where I have colums
word
grammatic
translation
language
and unit.
I need to get all words (values from column called word) to be able to create some sort of test.
User will choose from which language (for example english) and from which unit (for example weather) he wants to be tested.
After pressing a button called Test, another form will be opened and the data in data grid view table will be filtered with two filters - language and unit (I managed to solve this problem). In the open form there will be two labels and a button. Instead of text of the first label there should be a word from the datagridview table, after pressing the button also translation should be shown - instead of text of the second label.
Could anyone help me how to do this?
I think something along the lines:
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
Console.WriteLine(dataGridView1.Rows[i].Cells["cname"].Value.ToString());
}
problem definition:
having data with symbolic colors (i.e each teacher is noted with a color)
in DataGridView displaying a simple SELECT query result, I managed to color each row with its teacher's color
using visual studio 2012 and crystal report for VS 13.0.5
public void setColor() {
int index = this.Dgv.Columns["color"].Index;
foreach (DataGridViewRow row in this.Dgv.Rows)
{
System.Drawing.ColorConverter cc = new System.Drawing.ColorConverter();
System.Drawing.Color c = (System.Drawing.Color)cc.ConvertFromString(row.Cells[index].Value.ToString());
row.DefaultCellStyle.BackColor = c;
}
this.Dgv.ClearSelection();
}
I'd like to get a similarly styled report in crystal report but I'm having problems with color manipulation, the far I could get was alternating row color as many articles online help me through.
crystal report color issues I've found while searching were solved by applying the logic in a formula but in my case the colors are within the bound dataset (color values like "WHITE", "#FF0092" etc)
I thought of changing how the color is stored to (rr,gg,bb) to make use of the Color function in crystal report, would the function accept the string instead of 3 parameters?
to wrap my question up:
- is it possible to achieve the same level of color-control in crystal reports as in C#?
- is it possible to string-manipulate the (rr,gg,bb) string to extract the 3 needed parameters?
any pointers are appreciated
please include explanation since I'm a CR newbie
thank you
Not certain I understand the 2nd part. You can definitely achieve total color control.
Try a formula called Color. Define it as:
if {Teacher}='teacher1' then color(1,2,3)
else if ...
for each of the teachers.
A CASE statement will work as well.
Then in the Section Expert for the desired section select color, then 'more' to get to the formula expert. Simply insert the name of the formula you just created - #color
Is there a way to turn on grid lines in the entire datagridview client area rather than them becoming visible as you add rows?
I have read the msdn but find nothing of use.
C#, winforms, visual studio 2008
Thanks, R.
You could possibly subclass the DataGridView, override its PaintBackground event and add an image of some lines.
See here and here for some examples.
I realise this is a bit of a hack. :)
I'd choose between Andy's proposal, and the virtual mode.
In the virtual mode of grid view you will need to provide an interface to the data store by handling a number of events from the control. In such case you can set a RowCount property directly and then decide in the CellValueNeeded event if the cell contains data or not. You will have to determine though the number of rows that will fill your control.
http://msdn.microsoft.com/en-us/library/ms171622.aspx
You could add a heap of dummy rows to the grid and make them read only.
I faced that problem when I had number of rows in my datagrid what was taken from config file. In case of small number of the rows the datagrid looks ugly with gray background color. After discussion we desided to make datagrid height dynamic. That in its turn takes the parent window where the datagrid located to change its height as well. Now it looks pretty fine. This is not right answer to your question but rather
consider style issue.
int MAX_ROWS = 10;
int MAX_CELLS = 10;
dataGridView1.ColumnCount = MAX_CELLS;
int currentRowIndex = dataGridView1.Rows.Add(MAX_ROWS);
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
cell.ValueType = typeof(String);
cell.Value = "This is the: " + cell.OwningColumn.Index.ToString() +
" " + cell.OwningRow.Index.ToString() + " cell";
}
}
I know that it's not the best solution but it doesn't require making of a new DataGridView-like custom control with setting it's background image.