I'm trying to make a drop-down in a datagridview. I almost finished it. but I have some things that I do not know how to solve, for example in my datagridview, I have counters in the gray strip, this counts the quantity of products that there are in each group, if they do not see. it is called the inventory column. but inside these I have a value of 0 that I would like to hide if the value is 0, leave it blank, but I do not know how to do it. clear for the column Cant_Copeo and Cant_Avoid leave them blank and the accumulated or counted value is visible. This is the image of the datagridview displayed.
and it's closed.
in the code I am joining 2 data tables, one where the values are grouped and another where they contain pure records.
void CargarConDatatable() {
DataTable table = new DataTable("Customers");
table.Columns.Add("Accion", typeof(String));
table.Columns.Add("ID", typeof(Int32));
//workCol.AllowDBNull = false;
//workCol.Unique = false;
table.Columns.Add("Producto", typeof(String));
table.Columns.Add("Inventario", typeof(Int32));
table.Columns.Add("Copeo", typeof(bool));
table.Columns.Add("Cant_Copeo", typeof(Int32));
table.Columns.Add("Abierto", typeof(bool));
table.Columns.Add("Cant_Abierto", typeof(Int32));
// ID Producto Inventrio Copeo Cant_Copeo Abierto Cant_Abierto
table.Rows.Add("", 1, "Cola", 0, 1, 0, 1, 0);
table.Rows.Add("", 1, "Cola", 0, 1, 0, 1, 0);
table.Rows.Add("", 1, "Cola", 0, 1, 0, 0, 0);
table.Rows.Add("", 1, "Cola", 0, 1, 0, 1, 0);
table.Rows.Add("", 2, "Agua", 0, 1, 0, 1, 0);
table.Rows.Add("", 2, "Agua", 0, 1, 0, 1, 0);
table.Rows.Add("", 2, "Agua", 0, 1, 0, 1, 0);
table.Rows.Add("", 2, "Agua", 0, 1, 0, 1, 0);
table.Rows.Add("", 3, "Jugo", 0, 0, 0, 0, 0);
table.Rows.Add("", 3, "Jugo", 0, 0, 0, 0, 0);
table.Rows.Add("", 3, "Jugo", 0, 0, 0, 1, 0);
var t = from row in table.AsEnumerable()
group row by row.Field<Int32>("ID") into sales
orderby sales.Key
select new
{
Accion = "+",
ID = sales.Key,
Producto = sales.FirstOrDefault().Field<String>("Producto"),
Inventario = sales.Count(),
Cant_Copeo = sales.Count(x => x.Field<bool>("Copeo") == true),
Cant_Abierto = sales.Count(x => x.Field<bool>("Abierto") == true)
};
DataTable dt = Utilitied.ToDataTable(t.ToList());
var union = table.AsEnumerable().Union(dt.AsEnumerable()).OrderBy(d => d.Field<Int32>("ID"));
table.Merge(dt);
DataView dv = table.DefaultView;
dv.Sort = "ID ASC";
table = dv.ToTable();
this.dataGridView1.DataSource = table; // t.ToList() ;
this.dataGridView1.Columns["Cant_Copeo"].DefaultCellStyle.Format = "c";
this.dataGridView1.Columns["Cant_Abierto"].DefaultCellStyle.Format = "d";
this.dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.Coral;
this.dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;
this.dataGridView1.DefaultCellStyle.SelectionBackColor = Color.DarkSalmon;
this.dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Red;
//this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
}
// the code where the rows are hidden
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
Console.WriteLine("Completo");
for (int row = 0; row < dataGridView1.Rows.Count; row++)
{
for (int col = 0; col < dataGridView1.Columns.Count; col++)
{
if (dataGridView1.Rows[row].Cells[col].Value != null && dataGridView1.Rows[row].Cells[col].Value.Equals(dataGridView1.Rows[row].Cells[col].Value.ToString()))
{
if (dataGridView1.Rows[row].Cells[col].Value.Equals("+"))
{
dataGridView1.Rows[row].Cells[col].Style.BackColor = Color.Gray;
dataGridView1.Rows[row].DefaultCellStyle.BackColor = Color.LightGray;
//var cell = dataGridView1[0, row] = new DataGridViewTextBoxCell();
//cell.Style.BackColor = Color.ForestGreen;
}
}
}
if (!dataGridView1.Rows[row].Cells[0].Value.Equals("+"))
{
this.dataGridView1.CurrentCell = null;
this.dataGridView1.Rows[row].Visible = false;
var cell = dataGridView1[0, row] = new DataGridViewTextBoxCell();
cell.ReadOnly = true;
cell.Style.BackColor = Color.FromKnownColor(KnownColor.Control);
}
}
}
and where "the deployment" is done
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].Name == "Accion" && dataGridView1.Rows[e.RowIndex].Cells[0].Value.Equals("+"))
{
int index = e.RowIndex;
String ID = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
for (int row = 0; row < dataGridView1.Rows.Count; row++)
{
String Valor = dataGridView1.Rows[row].Cells[1].Value.ToString();
if (!this.dataGridView1.Rows[row].Visible && ID.Equals(Valor))
{
this.dataGridView1.Rows[row].Visible = true;
}
else if (this.dataGridView1.Rows[row].Visible && ID.Equals(Valor) && index != row && dataGridView1.DefaultCellStyle.BackColor != Color.LightGray)
{
this.dataGridView1.CurrentCell = null;
this.dataGridView1.Rows[row].Visible = false;
}
}
}
}
Thank you ..