How to merge cell in datagridview then add a combobox to cell was merged? - c++-cli

I have a function merge cell in datagridview in clr window form application as below:
void EquipmentDataScreen::MergeCellsInColumn(DataGridView^ datagrid,int col, int row1, int row2)
{
Graphics^ g = datagrid->CreateGraphics();
Pen^ p = gcnew Pen(datagrid->GridColor);
Rectangle r1 = datagrid->GetCellDisplayRectangle(col, row1, true);
Rectangle r2 = datagrid->GetCellDisplayRectangle(col, row2, true);
int recHeight = 0;
String^ recValue ="";
for (int i = row1; i <= row2; i++)
{
recHeight += datagrid->GetCellDisplayRectangle(col, i, true).Height;
if (datagrid->Rows[i]->Cells[col]->Value != nullptr)
recValue += datagrid->Rows[i]->Cells[col]->Value->ToString() + " ";
}
Rectangle newCell = Rectangle(r1.X, r1.Y, r1.Width-1, recHeight-1);
g->FillRectangle(gcnew SolidBrush(datagrid->DefaultCellStyle->BackColor), newCell);
g->DrawRectangle(p, newCell);
StringFormat^ strformat = gcnew StringFormat();
strformat->Alignment = StringAlignment::Center;
strformat->LineAlignment = StringAlignment::Center;
g->DrawString(recValue, datagrid->DefaultCellStyle->Font, gcnew SolidBrush(datagrid->DefaultCellStyle->ForeColor), newCell, strformat);
//g->DrawString(recValue, datagrid->DefaultCellStyle->Font, gcnew SolidBrush(datagrid->DefaultCellStyle->ForeColor), newCell.X + 3, newCell.Y + 3);
}
But this function simply draws a rectangle to cells that I want to merge.
The question is "is there a way to actually merge cell in datagridview then add combobox or text box to cell was merged?"
Please help me.
Thank you very much!

Related

Why the last empty row has been pulled as an additional test with null values in selenium apache POI

I have four rows in an excel, first row is for the heading and rest of the three rows has values in it. I have entered the code in a way to avoid the header and read the rows which contains values only. However instead of fetching only three rows it comes with one additional null value rows as below, Why it is fetching the null values did I miss anything? Find the code and error message.
Message
PASSED: testShipment("Mumbai", "New York", "18000", "10000", "20000")
PASSED: testShipment("Mumbai", "Cochin", "2000", "30000", "5000")
PASSED: testShipment("Cochin", "Farah", "16000", "18000", "19000")
FAILED: testShipment(null, null, null, null, null)
Code
int TotalCol = sh.getRow(0).getLastCellNum();
int Totalrows = sh.getLastRowNum()+1;
String[][] data = new String[Totalrows][TotalCol];
DataFormatter formatter = new DataFormatter(); // creating formatter using the default locale
for (int i = 1; i < Totalrows; i++) {
Row r = sh.getRow(i);
for (int j = 0; j < TotalCol; j++) {
Cell c = r.getCell(j);
try {
if (c.getCellType() == Cell.CELL_TYPE_STRING) {
String j_username = formatter.formatCellValue(c);
data[i][j] = j_username;
System.out.println("data[i][j]" + data[i][j]);
} else {
data[i][j] = String.valueOf(c.getNumericCellValue());
String j_username = formatter.formatCellValue(c);
data[i][j] = j_username;
System.out.println("data[i][j] numeric val" + data[i][j]);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Try with the below code like checking null condition
for (int k = 1; k <= totalRows; k++) {
String testCaseID = sheet.getRow(k).getCell(0).getStringCellValue();
if (testCaseID.equalsIgnoreCase(tcID)) {
for (int l = 1; l < totalCols; l++) {
String testData_FieldName = sheet.getRow(0).getCell(l).getStringCellValue();
if (testData_FieldName.equalsIgnoreCase(header)) {
cell = sheet.getRow(k).getCell(l);
if (cell != null) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:// numeric value in excel
result = cell.getNumericCellValue();
break;
case Cell.CELL_TYPE_STRING: // string value in excel
result = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN: // boolean value in excel
result = cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_BLANK: // blank value in excel
result = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_ERROR: // Error value in excel
result = cell.getErrorCellValue() + "";
break;
default:
throw new CustomException("The cell data type is invalid");
}
}
}
}
k = totalRows + 1;
}
}
You need to change either the data array declaration part or Totalrows calculation part. Currently, you have created 4 rows object and only 3 rows values are assigned and hence 4th row values are holding null value.
String[][] data = new String[Totalrows][TotalCol];
In your string array, you are not persisting the header value and storing only values. So, please modify your code with any one of the below options (I would suggest you to use option 1)
Option 1:
Remove the +1 from Totalrows variable and add the equal condition in your first for loop
//Removed the +1
int Totalrows = sh.getLastRowNum();
String[][] data = new String[Totalrows][TotalCol];
DataFormatter formatter = new DataFormatter(); // creating formatter using the default locale
//Condition is modified as i <= Totalrows
for (int i = 1; i <= Totalrows; i++) {
Option 2:
Change the data[][] declaration part
int Totalrows = sh.getLastRowNum()+1;
String[][] data = new String[Totalrows-1][TotalCol];
Here is the code that works, thanks to everyone for helping on this!
int TotalCol = sh.getRow(0).getLastCellNum();
int Totalrows = sh.getLastRowNum()+1;
//Entering minus one(-1) during data declaration ignores the first row as first row is a header
String[][] data = new String[Totalrows-1][TotalCol];
DataFormatter formatter = new DataFormatter(); // creating formatter using the default locale
for (int i = 1; i <Totalrows; i++) {
Row r = sh.getRow(i);
for (int j = 0; j < TotalCol; j++) {
Cell c = r.getCell(j);
try {
if (c.getCellType() == Cell.CELL_TYPE_STRING) {
String j_username = formatter.formatCellValue(c);
//Adding minus on(data[i-1]) helps to read the first cell which is (0,1), in this case (0,1) would not read the header since we have skipping the header from the table in the previous step on top, therefore the actual table starts from the second row.
data[i-1][j] = j_username;
System.out.println("data[i-1][j]" + data[i-1][j]);
} else {
data[i-1][j] = String.valueOf(c.getNumericCellValue());
String j_username = formatter.formatCellValue(c);
data[i-1][j] = j_username;
System.out.println("data[i-1][j] numeric val" + data[i-1][j]);
}
} catch (Exception e) {
e.printStackTrace();
}
}
"

Get Data from GridView to RdotNet (R.net)

my task is simple i just wanna take data from gridview to REngine do random function get back rows and show them back in the gridview, I tried that
TextBox_Ville.Text = "I'm here";
DataTable dtb = (DataTable)Session["Grid"];
REngine engine = REngine.GetInstance();
string[,] stringData = new string[dtb.Rows.Count, dtb.Columns.Count];
for (int row = 0; row < dtb.Rows.Count; row++)
{
for (int col = 0; col < dtb.Columns.Count; col++)
{
stringData[row, col] = dtb.Rows[row].ItemArray[col].ToString();
}
}
CharacterMatrix matrix = engine.CreateCharacterMatrix(stringData);
engine.SetSymbol("myRDataFrame", matrix);
engine.Evaluate("myRDataFrame <- as.data.frame(myRDataFrame, stringsAsFactors = FALSE)");
// engine.Evaluate("str(myRDataFrame)");
DataFrame dataset = engine.Evaluate("myRDataFrame[sample(nrow(myRDataFrame), 1), ]").AsDataFrame();
DataTable dtt = new DataTable();
for (int i = 0; i < dataset.ColumnCount; ++i)
{
dtt.Columns.Add(new DataColumn(dataset.ColumnNames[i]));
}
for (int i = 0; i < dataset.RowCount; ++i)
{
var row = dtt.NewRow();
for (int k = 0; k < dataset.ColumnCount; ++k)
{
row[dataset.ColumnNames[k]] = dataset[i, k];
}
dtt.Rows.Add(row);
}
GridView1.DataSource = dtt;
GridView1.DataBind();
But it give me Stackoverflow error, can anyone help please. thanks :)
I see you omitted type when building your DataTable. Perhaps that is the problem?
dtt.Columns.Add(new DataColumn(dataset.ColumnNames[i], typeof(string)));
The other thing I noticed is that you use 'row' as an iterator in a for loop, then you use it again to build a DataRow. I can't tell if it's a problem or not.

Conditional Formatting With Icons Epplus

I need to achieve something like this with Epplus.
Can someone guide me with the code I need to use.
Following is the code to do exactly what you want but it is for three icon set you can change it based on your icons. I'm setting red color arrow if value is greater than 4,yellow color arrow if value is between 1 and 4 and, finally, green color if it is less than 1. Just change "AddThreeIconSet" to your icons. You should get the idea with this.
for (int j = 2; j <= 9; j++) // Loop through columns
{
for (int i = 3; i <= 12; i++) // Loop through rows
{
// gets only the current cell as range
ExcelRange rng = worksheet.Cells[i, j, i, j];
ExcelAddress address = new ExcelAddress(rng.Address);
// Get the value of the current cell
if(Convert.ToDouble(worksheet.Cells[i, j].Value) >= 4.0)
{
var v = worksheet.ConditionalFormatting.AddThreeIconSet(address, eExcelconditionalFormatting3IconsSetType.Arrows);
v.Reverse = true;
v.Icon1.Type = eExcelConditionalFormattingValueObjectType.Num;
}
else if (Convert.ToDouble(workSheet.Cells[i, j].Value) > 1.0 && Convert.ToDouble(workSheet.Cells[i, j].Value) < 4.0)
{
var v = worksheet.ConditionalFormatting.AddThreeIconSet(address , eExcelconditionalFormatting3IconsSetType.Arrows);
v.Icon3.Type = eExcelConditionalFormattingValueObjectType.Num;
}
else if (Convert.ToDouble(workSheet.Cells[i, j].Value) < 1.0)
{
var v = worksheet.ConditionalFormatting.AddThreeIconSet(address , eExcelconditionalFormatting3IconsSetType.Arrows);
v.Icon2.Type = eExcelConditionalFormattingValueObjectType.Num;
}
}
}

Zedgraph - Change X-Axis from points to frequency

i have a working program where i can add an array to a Zedgraph and show this in a form.
Now i only want to change the display of the x-axis from points (0..400) to frequency (9e3..6e9 Hz).
Here are my currently working functions:
public void AddGraph(double[] Values, string LegendName)
{
int i = 0;
PointPairList list = new PointPairList();
for (i = 0; i < Values.Length; i++)
{
list.Add(i, Values[i]);
}
if (i > MaxXAxis)
MaxXAxis = i;
SList.Add(list);
SListColor.Add(Color.Black);
}
SListName.Add(LegendName);
}
public void ShowDiagram(string Title, string XAxisName, string YAxisName,
int Timeout_ms)
{
ZedGraph.ZedGraphControl zgc = new ZedGraphControl();
GraphPane myPane = zgc.GraphPane;
LineItem myCurve = null;
// Set the titles and axis labels
myPane.Title.Text = Title;
myPane.XAxis.Title.Text = XAxisName;
myPane.YAxis.Title.Text = YAxisName;
for (int i = 0; i < SList.Count(); i++)
{
myCurve = myPane.AddCurve(SListName[i], SList[i], SListColor[i],
SymbolType.None);
myCurve.Line.Width = 2;
}
// Add gridlines to the plot, and make them gray
myPane.XAxis.MinorGrid.IsVisible = true;
myPane.YAxis.MinorGrid.IsVisible = true;
myPane.XAxis.MinorGrid.Color = Color.LightGray;
myPane.YAxis.MinorGrid.Color = Color.LightGray;
myPane.XAxis.MinorGrid.DashOff = 0;
myPane.YAxis.MinorGrid.DashOff = 0;
myPane.XAxis.MajorGrid.IsVisible = true;
myPane.YAxis.MajorGrid.IsVisible = true;
myPane.XAxis.MajorGrid.Color = Color.Gray;
myPane.YAxis.MajorGrid.Color = Color.Gray;
myPane.XAxis.MajorGrid.DashOff = 0;
myPane.YAxis.MajorGrid.DashOff = 0;
// Move Legend to bottom
myPane.Legend.Position = LegendPos.Bottom;
zgc.AxisChange();
myPane.XAxis.Scale.Max = MaxXAxis;
zgc.Location = new Point(0, 0);
zgc.Size = new Size(panel_diagramm.ClientRectangle.Width, panel_diagramm.ClientRectangle.Height);
panel_diagramm.Controls.Add(zgc);
}
How can i change the above two functions that they display the frequency in the x-axis?
I already tried to change the AddGraph-function to pass the needed parameters and to calculate the list to have the correct values. But what then...?
public void AddGraph_Frequency(int **Points**, double **StartFrequency**,
double **StopFrequency**, double[] Values, string GraphColor, string LegendName)
{
...
double frequency = StartFrequency; //der erste Punkt
double Intervall = (StopFrequency - StartFrequency) / Points;
for (i = 0; i < Points; i++)
{
list.Add(frequency, Values[i]);
frequency = frequency + Intervall;
}
....
}
Thanks for any help
best regards
Solved.
Missing was:
myPane.XAxis.Scale.Max = Stopfrequency;
myPane.XAxis.Scale.Min = Startfrequency;

Draw three grids of user controls in VB.NET

I have one user control of size 25x25 and I want to duplicate it into three separate 10x10 grids which I can change the location of on my form.
I'm making a pandemic simulation so the three grids represent the three countries and I will change the colour of the user control depending on the infection status of the grid square.
I've been playing around with this for a long time but I cannot get it to work, when I use Me.Controls.Add(UserControl) it overwrites the previous one and I'm left with only one user control on the form.
Any help appreciated, thanks.
Below is a method that will create grid and you can place arbitrary controls into "cells" of this "grid".
To run this, paste this code into any button handler and it will do everything it needs.
I don't know if this is exact solution but you may get something out of it. It would be helpful if you could make a mock of your screen in different states, so we understand what you actually want.
Call method DoAGrid on some button handler:
DoAGrid(bool.Parse(_txtTest.Text)); // type true or false in txt
The methos
private void DoAGrid(bool isTest)
{
const int size = 30; // I give 2 for controll padding
const int padding = 20; // x and y starting padding
Point[,] grid = new Point[10,10]; // x and y of each control
List<Control> btns = null;
if (isTest) btns = new List<Control>(100);
for (int x = 1; x < 11; x++)
{
for (int y = 1; y < 11; y++)
{
grid[x - 1, y - 1] = new Point(padding + x*size - 30 - 1, padding + y*size - 30 - 1); // 30 - 1 --> size + 2
if (isTest)
{ // this test will add all avail buttons so you can see how grid is formed
Button b = new Button();
b.Size = new Size(size, size);
b.Text = "B";
b.Location = grid[x - 1, y - 1];
btns.Add(b);
}
}
}
Form f = new Form();
f.Size = new Size(1000, 1000);
if (isTest)
{
f.Controls.AddRange(btns.ToArray());
}
else
{
// Add controls to random grid cells
Button b1 = new Button();
b1.Size = new Size(size, size);
b1.Text = "B1";
b1.Location = grid[3, 3];
Button b2 = new Button();
b2.Size = new Size(size, size);
b2.Text = "B2";
b2.Location = grid[5, 5];
Button b3 = new Button();
b3.Size = new Size(size, size);
b3.Text = "B3";
b3.Location = grid[8, 8];
Button b4 = new Button();
b4.Size = new Size(size, size);
b4.Text = "B4";
b4.Location = grid[8, 9];
Button b5 = new Button();
b5.Size = new Size(size, size);
b5.Text = "B5";
b5.Location = grid[9, 8];
Button b6 = new Button();
b6.Size = new Size(size, size);
b6.Text = "B6";
b6.Location = grid[9, 9];
f.Controls.AddRange(new Button[] { b1, b2, b3, b4, b5, b6 });
}
f.ShowDialog();
}