I have to display a list of checkboxes (50) from a table A, in this list i have to mark as true boxes that are checked depending on elements in table B.
The goal is to have all the 50 checkboxes with those checked.
Can you guys help me please.
int envid = int.Parse(txtenvid.Text);
DataView oDvPop = PopulationBL.BigList();
DataView listPop = PopulationBL.SmallListCheckeditems(envid, 1);
CheckBoxList list = new CheckBoxList();
CheckBox check;
CheckBox check2;
for (int i = 0; i < listPop.Count; i++)
{
check = new CheckBox();
check2 = new CheckBox();
check.Text = listPop[i]["POP"].ToString();
for (int j = 0; j < oDvPop.Count; j++)
{
check2.Text = oDvPop[j]["POP_LABEL"].ToString();
if (check.Text == check2.Text)
check.Checked = true;
checkBoxes.Add(check);
chkListPop.Controls.Add(check);
}
}
chkListPop.Controls.Add(check);
First of all you are doing
CheckBox check = new CheckBox();
CheckBox check2 = new CheckBox();
outside the loop so will have the last values assign in all of checkboxes. So insatiate it inside loop.
CheckBox check;
CheckBox check2;
for (int i = 0; i < listPop.Count; i++)
{
check = new CheckBox();
check2 = new CheckBox();
}
Related
I read some data from a datagrid, named dgTable2(3 Columns, unknown Rows). After that, i want to make for every row inside dgTable2, a new column inside dgTable3 and as headername i want to set the first parameter of dgTable2 (column 0 in all rows).
My Problem is to add a new DatagridColumn dynamically.
Here my Code:
private void LoadDataToMaschine_Click(object sender, RoutedEventArgs e)
{
dgTable1.Visibility = Visibility.Collapsed;
dgTable3.Visibility = Visibility.Visible;
int anzZeilen = tableSelectedVar.Rows.Count;
string[,] inhaltVar = new string[anzZeilen, 3];
//read all data from table to array
for (int i = 0; i <= anzZeilen-1; i++)
{
for (int j = 0; j < 3; j++)
{
inhaltVar[i, j] = tableSelectedVar.Rows[i].ItemArray[j].ToString();
}
}
//create a new table for the Data
DataTable tableforDataStream = new DataTable();
//Should create a datagridcolumn and add a tableColumn
for (int k = 0; k <= (inhaltVar.GetLength(0)-1); k++)
{
//Erstellt die Columns für jeden Parameter
dgTable3.Columns.Add(inhaltVar[k,0]);
tableforDataStream.Columns.Add(inhaltVar[k, 0]);
}
dgTable3.ItemsSource = tableforDataStream.DefaultView;
}
It's simple as f... :D
DataGridTextColumn textColumn = new DataGridTextColumn();
textColumn.Header = "Header";
textColumn.Binding = new Binding("BindingValue"); //only if you want to bind data to it
dataGrid.Columns.Add(textColumn);
I want to create a datagridview and set first row to be filter for each column. Example: Under header i need to have an editable cell. And each time i will press something in this cell to refresh datagridview with items which are like my text-value. I searched in web i didn't found anything.
DataTable dt1 = new DataTable();
dt1.Columns.Add("a");
dt1.Columns.Add("b");
for (int j = 0; j < 10; j++)
{
dt1.Rows.Add("a" + j.ToString(), "b" + j.ToString());
}
this.dataGridView1.DataSource = dt1;
//I take a TextBox for example
TextBox tx = new TextBox();
//Add this TextBox into the DataGridView's control collection
this.dataGridView1.Controls.Add(tx);
//Add an extra row into the data source
dt1.Rows.InsertAt(dt1.NewRow(), 0);
//Make the first row frozen.
this.dataGridView1.Rows[0].Frozen = true;
//Resize the TextBox and put it over the first row
Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(0, 0, true);
tx.Location = rect.Location;
int w = 0;
foreach (DataGridViewColumn c in this.dataGridView1.Columns)
{
w += c.Width;
}
tx.Width = w;
tx.Height = this.dataGridView1.Rows[0].Height;
}
}
I have a question regarding creating controls in runtime in ASP.NET 4.0. I'm building a application and in admin.aspx page I have multiple controls (DropDownLists) which are dynamically created with values from a Sql database.
I know that for having events fired for dynamically created controls, I have to create this controls in Page_Load() or in Page_Init().
My project has a Master Page, in which I have a 1 second timer which updates a clock. This timer event calls my admin.aspx Page_Load() function, so my method which creates dynamic controls it's called every 1 second - connection to database is made every one second, please look below to my code.
It's a good practice to do that? Can you please propose some ideas?
protected void Page_Load(object sender, EventArgs e)
{
_SinopticBackgroundColor = ConfigurationManager.AppSettings["SinopticBackgroundColor"];
panelContent.Style.Add("background-color", _SinopticBackgroundColor);
Control c = GetControlThatCausedPostBack(this);
if (c != null)
{
if (c.ID.Equals("btnManageCategory"))
hfPageManage.Value = "category";
else if (c.ID.Equals("btnManageDevices"))
hfPageManage.Value = "device";
}
if (hfPageManage.Value.Equals("category"))
{
cbTreeViewGroup.Visible = false;
UpdateSinopticCategoryManager(TreeView1.SelectedNode); //this is the functions which loads controls from database..
}
else if (hfPageManage.Value.Equals("device"))
{
cbTreeViewGroup.Visible = true;
}
else
{
cbTreeViewGroup.Visible = false;
}
if (!Page.IsPostBack)
{
LoadFunctions(); // loads some values from database into datatables
}
else
{
}
}
And here is the functions which creates controls
private void UpdateSinopticCategoryManager(TreeNode node = null)
{
if (node == null)
return;
DataTable categoriiDT = null;
using (var connection = new SqlConnection(Database.ConnectionString))
{
using (var command = connection.CreateCommand())
{
command.CommandText = "SELECT * FROM categories WHERE CategoryID = #CategoryID";
command.Parameters.Add("CategoryID", node.Value);
SqlDataAdapter ad = new SqlDataAdapter(command);
DataSet ds = new DataSet("CATEGORYPROPERTIES");
connection.Open();
ad.Fill(ds);
// verificam sa avem date
if (ds.Tables.Count <= 0)
return;
if (ds.Tables[0].Rows.Count <= 0)
return;
categoriiDT = ds.Tables[0];
}
}
// generate table
Table table = new Table();
table.Style.Add("position", "relative");
table.Style.Add("top", "20px");
table.Style.Add("margin-left", "20px");
table.BorderStyle = BorderStyle.Solid;
table.BorderWidth = 1;
// header
TableHeaderRow hr = new TableHeaderRow();
for (int i = 0; i < 2; i++)
{
TableHeaderCell hc = new TableHeaderCell();
if (i > 0)
{
hc.Text = "FUNCTION";
//hc.Width = 200;
}
else
{
hc.Width = 100;
}
hr.Cells.Add(hc);
}
table.Rows.Add(hr);
var inputs = (from a in categoriiDT.Columns.Cast<DataColumn>()
where a.ColumnName.ToLowerInvariant().Contains("input")
select a.ColumnName).ToArray();
if (inputs.Count() <= 0)
return;
//rows input
for (int i = 0; i < inputs.Count(); i++)
{
TableRow tableRow = new TableRow();
for (int j = 0; j < 2; j++)
{
TableCell cell = new TableCell();
if (j > 0)
{
// adaugare 2 dropdownlist
DropDownList categList = new DropDownList();
categList.SelectedIndexChanged += new EventHandler(categList_SelectedIndexChanged);
foreach (DataRow row in functionsCategories.Rows)
{
categList.Items.Add(new ListItem(row["FunctionCategoryName"].ToString(), row["FunctionCategoryID"].ToString()));
}
DropDownList funcList = new DropDownList();
int selF = 0, selC = 0;
for (int fi = 0; fi < functions.Rows.Count; fi++)// (DataRow row in functions.Rows)
{
funcList.Items.Add(new ListItem(functions.Rows[fi]["FunctionName"].ToString(), functions.Rows[fi]["FunctionID"].ToString()));
if (functions.Rows[fi]["FunctionID"].ToString() == categoriiDT.Rows[0][inputs[i]].ToString())
{
selF = fi;
selC = Int32.Parse(functions.Rows[fi]["FunctionCategoryID"].ToString());
}
}
funcList.SelectedIndex = selF;
categList.SelectedIndex = functionsCategories.Rows.IndexOf(
(from c in functionsCategories.AsEnumerable()
where c["FunctionCategoryID"].ToString().Equals(selC.ToString())
select c).FirstOrDefault());
cell.Controls.Add(categList);
cell.Controls.Add(funcList);
}
else
{
Label label = new Label();
label.Text = "INPUT " + i.ToString();
label.Style.Add("font-weight", "bold");
cell.Controls.Add(label);
}
tableRow.Cells.Add(cell);
}
table.Rows.Add(tableRow);
}
//rows output
for (int i = 0; i < 4; i++)
{
TableRow row = new TableRow();
for (int j = 0; j < 2; j++)
{
TableCell cell = new TableCell();
if (j > 0)
{
DropDownList list = new DropDownList();
list.Width = 200;
list.Items.AddRange(GetOutputFunctions());
list.BorderColor = Color.Goldenrod;
cell.Controls.Add(list);
}
else
{
Label label = new Label();
label.Text = "OUTPUT " + i.ToString();
label.Style.Add("font-weight", "bold");
cell.Controls.Add(label);
}
row.Cells.Add(cell);
}
table.Rows.Add(row);
}
// add table to panel
panelContent.Controls.Add(table);
}
It's about this: DropDownList categList = new DropDownList();
This may get answered more quickly and effectively in codereview https://codereview.stackexchange.com/
My question is: When I click some Checkbox, how can I get the current checkbox control's index from DataGridView
Here is my snick code
dataGridView2.RowCount = 5;
dataGridView2.ColumnCount = 4;
for (int i = 0; i < dataGridView2.ColumnCount; i++)
{
for (int j = 0; j < dataGridView2.RowCount; j++)
{
box = new CheckBox();
box.Text = "MyDate";
//box.Size = new System.Drawing.Size(15, 15);
dataGridView2.Controls.Add(box);
Rectangle rec = dataGridView2.GetCellDisplayRectangle(i, j, true);
box.Left = rec.Left;
box.Top = rec.Top;
}
}
}
It looks like that you try adding pure CheckBoxes to your DataGridView without using a DataGridViewCheckBoxColumn, the solution for this approach is simple like this:
for (int i = 0; i < dataGridView2.ColumnCount; i++)
{
for (int j = 0; j < dataGridView2.RowCount; j++)
{
box = new CheckBox();
box.Text = "MyDate";
//box.Size = new System.Drawing.Size(15, 15);
dataGridView2.Controls.Add(box);
Rectangle rec = dataGridView2.GetCellDisplayRectangle(i, j, true);
box.Left = rec.Left;
box.Top = rec.Top;
//Added code
box.Tag = new Point(i,j);
box.Click += CheckBoxesClicked;
}
}
private void CheckBoxesClicked(object sender, EventArgs e){
CheckBox chb = sender as CheckBox;
if(chb.Tag != null) {
Point coord = (Point)chb.Tag;
MessageBox.Show(string.Format("Row index: {0}\nColumn index: {1}", coord.Y, coord.X);
}
}
You should use a DataGridViewCheckBoxColumn instead, with that approach, you can handle the event CellContentClick...
if you are using the CellContentClick event or any other event that you get the DataGridViewCellEventArgs then you have ColumnIndex and RowIndex properties that are the column and row of the cell changed
Check this links. This gives you details about grid view.
http://msdn.microsoft.com/en-us/library/ms972814.aspx
http://msdn.microsoft.com/en-us/library/aa479344.aspx
I'm trying this code:
arrList = new List<CheckBox>();
for (int j = 0; j < 20; j++)
{
CheckBox check = new CheckBox();
arrList.Add(check);
}
CheckBox[] cb = arrList.ToArray();
for (int i = 0; i < 20; i++)
{
cb[i].Text = "sometext";
cb[i].Location = new System.Drawing.Point(10, 15 + i * 20);
cb[i].BackColor = System.Drawing.Color.Silver;
cb[i].Name = "somename";
cb[i].Size = new System.Drawing.Size(59, 17);
cb[i].Checked = true;
groupBox1.Controls.Add(cb[i]);
}
How can I add a vertical scroll bar there? (maybe I should use something instead of groupbox?)
You could use a Panel control and set it's AutoScroll property to True