I am trying to output the cell values of a data grid to an XML . The cells of the grid are combobox cells with values ALLOCATED, AVAILABLE, OCCUPIED. My xml output needs to be of the form as stated below: (where status is the selected value from the combo box of each cell and Xloc is the row index , YLoc is the column index of that particular cell)
<Cell XLoc="1" YLoc="1" Status="AVAILABLE"/>
<Cell XLoc="1" YLoc="2" Status="OCCUPIED"/>
<Cell XLoc="1" YLoc="3" Status="OCCUPIED"/>
Currently I am creating a list and saving the combox selected values from the grid ie., AVAILABLE, OCCUPIED....
private List<string> Grid_Values()
{
List<string> CellValues = new List<string>();
for (int i = 0; i < ToolMapGrid.Rows.Count; i++)
{
for (int j = 0; j < ToolMapGrid.ColumnCount; j++)
{
CellValues.Add(ToolMapGrid.Rows[i].Cells[j].Value.ToString());
}
}
...
}
I am writing the above values to XML in this way :
List<string> GridValues = Grid_Values();
foreach (string item in GridValues)
{
writer.WriteStartElement("Cell");
writer.WriteAttributeString("Status",item);
writer.WriteEndElement();
}
I dont understand how do I write Xloc and Yloc values to the XML file. I am returning the combo values as a list but how do I make the same method also return the row and column indexes and how do I write it to the XML file.
Please help me. Thanks in advance
Create a new class called XmlInfo which has three properties, like this:
class XmlInfo
{
string value{get; set;}
int column {get: set;}
int row {get; set; }
}
After that change your code like this:
private List<XmlInfo> Grid_Values()
{
List<XmlInfo> CellValues = new List<XmlInfo>();
for (int i = 0; i < ToolMapGrid.Rows.Count; i++)
{
for (int j = 0; j < ToolMapGrid.ColumnCount; j++)
{
XmlInfo nfo = new XmlInfo {
value = ToolMapGrid.Rows[i].Cells[j].Value.ToString(),
row = i,
column = j}
CellValues.Add(nfo);
}
}
}
Then you can use the code like this:
List<XmlInfo> GridValues = Grid_Values();
foreach (XmlInfo item in GridValues)
{
writer.WriteStartElement("Cell");
writer.WriteAttributeString("Status", item.value);
writer.WriteAttributeString("XLoc", item.column);
writer.WriteAttributeString("YLoc", item.row);
writer.WriteEndElement();
}
You might want to review this for syntax errors, as I typed it directly into the editor and I might have missed something.
This is how I implemented and it worked :)
for (int i = 0; i < ToolMapGrid.Rows.Count; i++)
{
for (int j = 0; j < ToolMapGrid.ColumnCount; j++)
{
writer.WriteStartElement("Cell");
writer.WriteAttributeString("XlOC", (i + 1).ToString());
writer.WriteAttributeString("YLOC", (j + 1).ToString());
writer.WriteAttributeString("Status", ToolMapGrid.Rows[i].Cells[j].Value.ToString());
writer.WriteEndElement();
}
}
Related
My code is save datagrid to csv. when process to
value = dr.Cells[i].Value.ToString();
Error message is the following:System.Windows.Forms.DataGridViewCell.Value.get return null.
Then, I add corner case to check cell is null and replace those cells by "Null".
foreach (DataGridViewRow rw in this.dataGridView1.Rows)
{
for (int i = 0; i < rw.Cells.Count; i++)
{
if (rw.Cells[i].Value == System.DBNull.Value)
{
swOut.Write("Null");
}
}
}
But the error message is there still.
The following is my code:
public void writeCSV(DataGridView gridIn, string outputFile)
{
//test to see if the DataGridView has any rows
if (gridIn.RowCount > 0)
{
string value = "";
DataGridViewRow dr = new DataGridViewRow();
StreamWriter swOut = new StreamWriter(outputFile);
foreach (DataGridViewRow rw in this.dataGridView1.Rows)
{
for (int i = 0; i < rw.Cells.Count; i++)
{
if (rw.Cells[i].Value == System.DBNull.Value)
{
swOut.Write("Null");
}
}
}
//write header rows to csv
for (int i = 0; i <= gridIn.Columns.Count - 1; i++)
{
if (i > 0)
{
swOut.Write(",");
}
swOut.Write(gridIn.Columns[i].HeaderText);
}
swOut.WriteLine();
//write DataGridView rows to csv
for (int j = 0; j <= gridIn.Rows.Count - 1; j++)
{
if (j > 0)
{
swOut.WriteLine();
}
dr = gridIn.Rows[j];
for (int i = 0; i <= gridIn.Columns.Count - 1; i++)
{
if (i > 0)
{
swOut.Write(",");
}
value = dr.Cells[i].Value.ToString();
//replace comma's with spaces
value = value.Replace(',', ' ');
//replace embedded newlines with spaces
value = value.Replace(Environment.NewLine, " ");
swOut.Write(value);
}
}
swOut.Close();
}
}
The current code appears to work… IF the DataGridView.AllowUserToAddRows is false! The default is true. If the grid allows users to add rows, the code will crash at the line…
value = dr.Cells[i].Value.ToString();
when it hits the “new” row. The cells in the “new” row are null not DBNull. If you want to allow the user to add rows (which I assume is the case since the code is stripping out commas and new lines), then the code will need to check for this “new” row and ignore it when exporting the grid.
With that said, I believe you are making this way more complicated than it has to be. The goal is to export the cells in a DataGridView to a comma delimited file (CSV). This can be done with much less code and still avoid the dreaded null values in the cells.
From the perspective of the CSV file, if a cell is “null” that means that we want to output an “empty” string to the CSV file. This will maintain the column schema. Therefore, a simple double loop through the grids cells is all that needs to be done. While looping through the cells and writing the values to the file, a simple check is needed before we try and grab a cells value.ToString(). If value is null, the code will crash as a null does not have a ToString() method. Therefore if value “is” null then write an empty string to the file… problem solved!
Therefore, to help, I recommend you use a different strategy to export the grids cells. There appears no reason for the dr variable nor the value variable. In addition, I would assume that the cells text does “NOT” contain commas (,). If you “know” the grid is going to be exported to a CSV file… I would set the grids cells such that the user would “not” be able to type a comma. Therefore, the code below does not strip out commas or new lines. Hope that makes sense.
public void writeCSV(DataGridView gridIn, string outputFile) {
try {
using (StreamWriter swOut = new StreamWriter(outputFile)) {
//write header rows to csv
for (int i = 0; i < gridIn.Columns.Count; i++) {
swOut.Write(gridIn.Columns[i].HeaderText);
if (i < gridIn.ColumnCount - 1) {
swOut.Write(",");
}
else {
swOut.WriteLine();
}
}
//write DataGridView rows to csv
for (int row = 0; row < gridIn.Rows.Count; row++) {
if (!gridIn.Rows[row].IsNewRow) {
for (int col = 0; col < gridIn.Columns.Count; col++) {
if (dataGridView1.Rows[row].Cells[col].Value != null) {
swOut.Write(dataGridView1.Rows[row].Cells[col].Value.ToString());
}
else {
swOut.Write("");
}
if (col < gridIn.Columns.Count - 1) {
swOut.Write(",");
}
else {
swOut.WriteLine();
}
}
}
}
}
}
catch (Exception e) {
MessageBox.Show("Error: " + e.Message);
}
}
I have a function that sets column values for all rows:
The code that sets this:
//Update the engineers for all rows
Btn_ValidateClick_ItemClick(object sender,ItemClickEventArgs e)
{
UpdateTotalTime(gridView);
}
private void UpdateEngineers(DevExpress.XtraGrid.Views.Base.ColumnView View)
{
//Column name that need to be updated (set)
DevExpress.XtraGrid.Columns.GridColumn col = View.Columns.ColumnByFieldName("Engineers");
try
{
int dataRowCount = View.DataRowCount;
for (int i = 0; i < dataRowCount; i++)
{
GridView detail = (GridView)gridView.GetDetailView(i, 0);
string language = gridView.GetRowCellValue(i, "Language").ToString();
for (int y = 0; y < gridView.GetDetailView(i, 0).RowCount; y++)
{
//Add all values found in a detail column to an arraylist
values.Add(detail.GetRowCellValue(y, "EngineerInitials").ToString());
}
if (values.Count >0 )
object t = //string join ...
View.SetRowCellValue(i, col, t);
}
else
{
object t = "No engineers"
View.SetRowCellValue(i, col, t);
}
}
}
}
}
The problem is that now, I want it only to set it for the rows that are selected.
I tried using the .GetSelectedRows()-function and adding the rows to an ArrayList, but this doesn't allow customability really:
private void UpdateTotalTime(DevExpress.XtraGrid.Views.Base.ColumnView View)
{
ArrayList selectedRows = new ArrayList();
for (int i = 0; i < gridView.SelectedRowsCount; i++)
{
if (gridView.GetSelectedRows()[i] >= 0)
selectedRows.Add(gridView.GetDataRow(gridView.GetSelectedRows()[i]));
}
try
{
int count = View.GetSelectedRows().Count();
for (int i = 0; i < selectedRows.Count; i++)
{
//This gets the first row of the count, not the first selected row
GridView detail = (GridView)gridView.GetDetailView(i,0);
}
}
If I select the 3 bottom rows, the first 3 get updated. Why is this?
You are adding all the selected rows to your selectedRows ArrayList. But after that, you are not using it for anything.
I guess what you want (I've never used devexpress controls) is using those selectedrows RowHandle to pass it to the GetDetailView method. According to the GetSelectedRows documentation, the method returns the int handles of the selected rows, so your code should look like this:
First, you must save the DataRow handles, not the DataRow itself, so you must change in your code this line:
selectedRows.Add(gridView.GetDataRow(gridView.GetSelectedRows()[i]));
into this:
selectedRows.Add(gridView.GetSelectedRows()[i]);
And then, change your loop into this:
for (int i = 0; i < selectedRows.Count; i++)
{
int rowHandle = (int)selectedRows[i];
GridView detail = (GridView)gridView.GetDetailView(rowHandle,0);
}
In fact, you could do everything in just one loop:
private void UpdateTotalTime(DevExpress.XtraGrid.Views.Base.ColumnView View)
{
for (int i = 0; i < gridView.SelectedRowsCount; i++)
{
int rowHandle = gridView.GetSelectedRows()[i];
GridView detail = (GridView)gridView.GetDetailView(rowHandle,0);
}
}
I am trying to populate TextBoxes from a list. I have been able to populate ComboBoxes with comboList:
var comboList = new System.Windows.Forms.ComboBox[4];
comboList[0] = cmbSite1Asset;
comboList[1] = cmbSite2Asset;
comboList[2] = cmbSite3Asset;
comboList[3] = cmbSite4Asset;
List<CRCS.CAsset> assets = _rcs.Assets;
foreach (CRCS.CAsset asset in assets)
{
string id = asset.ID;
for (int i = 0; i < 4; ++i)
{
comboList[i].Items.Add(id);
}
}
But when I try and apply the same principle to TextBoxes
var aosList = new System.Windows.Forms.TextBox[8];
aosList[0] = txtAsset1;
aosList[1] = txtAsset2;
aosList[2] = txtAsset3;
aosList[3] = txtAsset4;
aosList[4] = txtAsset5;
aosList[5] = txtAsset6;
aosList[6] = txtAsset7;
aosList[7] = txtAsset8;
foreach (CRCS.CAsset asset in assets)
{
string id = asset.ID;
for (int n = 0; n < 8; ++n)
{
aosList[n].Items.Add(id);
}
}
TextBox does not like Items.Add ( aosList[n]Items.Add(id); )
I am looking fore a reference or guidance resolving this issue. Thanks!
You should use ComboBox for your problem,instead of iterating on each element,You simply use below lines to populate combobox.
comboList.DataSource=assets;
comboList.DisplayMember="ID";
comboList.ValueMember="ID";
However,if you want your values in TextBox,you can use TextBox.AppendText Method, but it will not work like ComboBox as it will contain texts+texts+texts, will not have indexes like ComboBox.
private void AppendTextBoxLine(string myStr)
{
if (textBox1.Text.Length > 0)
{
textBox1.AppendText(Environment.NewLine);
}
textBox1.AppendText(myStr);
}
private void TestMethod()
{
for (int i = 0; i < 2; i++)
{
AppendTextBoxLine("Some text");
}
}
A Combobox is a collection of items, and so has an Items property from which you can add/remove to change it's contents. A Textbox is just a control that displays some text value, so it has a Text property which you can set/get, and which denotes the string that is displayed.
System.Windows.Forms.TextBox[] aosList = new System.Windows.Forms.TextBox[8];
aosList[0] = txtAsset1;
aosList[1] = txtAsset2;
aosList[2] = txtAsset3;
aosList[3] = txtAsset4;
aosList[4] = txtAsset5;
aosList[5] = txtAsset6;
aosList[6] = txtAsset7;
aosList[7] = txtAsset8;
for (int n = 0; n < 8; ++n)
{
aosList[n].Text = assets[n].ID; // make sure you have 8 assets also!
}
int i = 1;
foreach (var asset in assets)
{
this.Controls["txtAsset" + i].Text = asset.ID;
i++;
}
So, i have a checklistbox that contains string values like:
asdf-432-qwer-vcxz
rewq-123-qwer-vcxz
rety-323-qw65-vcyt
kjhf-232-ouyy-bjkl
...
And i have an array(onlineVaults) that contains some of the same values that in checklistbox like:
rety-323-qw65-vcyt
asdf-432-qwer-vcxz
Now i want to check only those values in checklistbox that are in the array. Others should be unchecked.
for (int i = 0; i < checklistbox.Items.Count; i++)
{
if (onlineVaults.Contains(checklistbox.Items[i]))
{
checklistbox.SetItemChecked(i, true);
}
}
I have tried it to figure out like this, but it doesn't work. It only checks one value and nothing else. What i should do?
Try this
var checklistbox = new List<string>
{
"asdf-432-qwer-vcxz",
"rewq-123-qwer-vcxz",
"rety-323-qw65-vcyt",
"kjhf-232-ouyy-bjkl"
};
var onlineVaults = new List<string>
{
"rety-323-qw65-vcyt",
"asdf-432-qwer-vcxz"
};
for (int i = 0; i < checklistbox.Items.Count; i++)
{
checklistbox.SetItemChecked(i, onlineVaults.Contains(checklistbox.Items[i]));
}
EDIT:
To ignore case and trim values, try this
for (int i = 0; i < checklistbox.Items.Count; i++)
{
checklistbox.SetItemChecked(i, IsValueExist(onlineVaults, checklistbox.Items[i]));
}
private bool IsValueExist(List<string> list, string value)
{
return list.Any(x => string.Compare(x.Trim(), value.Trim(), StringComparison.InvariantCultureIgnoreCase) == 0);
}
I have been saving into the ComboBox a value out of the selected column in datagridview with below code.
My question is:How can I prevent duplicate records when I save the values into the ComboBox? How can I do that?
Code:
int ColumnIndex = dgUretimListesi.CurrentCell.ColumnIndex;
CmbAra.Text = "";
for (int i = 0; i < dgUretimListesi.Rows.Count; i++)
{
CmbAra.Items.Add(dgUretimListesi.Rows.Cells[ColumnIndex].Value.ToString());
}
Please try this
private void dgvServerList_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
try
{
if (e.ColumnIndex == 1)
{
string id = dgvServerList[e.ColumnIndex, e.RowIndex].Value.ToString();
int duplicaterow = 0;
for (int row = 0; row < dgvServerList.Rows.Count; row++)
{
if (row != e.RowIndex && id == dgvServerList[e.ColumnIndex, row].Value.ToString())
{
duplicaterow = row + 1;
MessageBox.Show("Duplicate found in the row: " + duplicaterow);
this.dgvServerList[e.ColumnIndex, e.RowIndex].Value = "";
break;
}
}
}
}
catch
{
}
}
you could first transfer your datagridview items to a dictionary (which guarantees uniqueness) and then transfer that dictionary content to the combobox. or you could check for uniqueness yourself using a 'Contains' method on the combobox. you could even tie the dictionary to the combobox as a source for the combobox items.
Dictionary<string,bool> d = new Dictionary<string,bool>();
int ColumnIndex = dgUretimListesi.CurrentCell.ColumnIndex;
CmbAra.Text = "";
for (int i = 0; i < dgUretimListesi.Rows.Count; i++)
{
d[dgUretimListesi.Rows.Cells[ColumnIndex].Value.ToString()] = true;
}
CmbAra.Items.AddRange(d.Keys);
Use a set:
int ColumnIndex = dgUretimListesi.CurrentCell.ColumnIndex;
CmbAra.Text = "";
HashSet<string> set = new HashSet<string>();
for (int i = 0; i < dgUretimListesi.Rows.Count; i++)
{
string s = dgUretimListesi.Rows.Cells[ColumnIndex].Value.ToString();
if(!set.Contains(s)) {
CmbAra.Items.Add(s);
set.Add(s);
}
}
by using the following check and then determine to add or not
if(CmbAra.Items.Contains(dgUretimListesi.Rows.Cells[ColumnIndex].Value.ToString()))
You can use the following code part.
if(!(CmbAra.Items.Contains(dgUretimListesi.Rows.Cells[ColumnIndex].Value.ToString())))
{
CmbAra.Items.Add(dgUretimListesi.Rows.Cells[ColumnIndex].Value.ToString());
}
else
{
MessageBox.Show("Value Already exists , not added");
}