//Code BehindFile
public void Button1_Click(object sender, EventArgs e)
{
while (reader.Read())
{
DropDownList ddl = new DropDownList();
string[] s = { "Present", "Absent1", "Absent2", "Absent3" };
for (int i = 0; i < 3; i++)
{
ddl.Items.Add(s[i]);
}
ddl.ID = "ddl";
TableCell c2 = new TableCell();
c2.Controls.Add(ddl);
r.Cells.Add(c2);
Table1.Rows.Add(r);
}
}
public void Button2_Click1(object sender, EventArgs e)
{
foreach (TableRow tr in Table1.Controls)
{
foreach (TableCell tc in tr.Controls)
{
if (tc.Controls[2] is DropDownList)
{
Response.Write(((DropDownList)tc.Controls[2]).SelectedItem.Text+" ");
}
}
Response.Write("<br/>");
}
Problem is with the Selection of dropdownlist items.I could not print the corresponding selected item values.could anyone help?
Check tc.Controls[2] when you are in the last nested foreach. Is it possible that your dropdown list is something other than the third control?
I don't see any reason that would force it to be the third control in that Cell.
You would probably be better off doing something like this:
if(tc.FindControl("ddl") != null)
{
Response.Write(((DropDownList)tc.FindControl("ddl")).SelectedItem.Text+" ");
}
instead of:
if (tc.Controls[2] is DropDownList)
{
Response.Write(((DropDownList)tc.Controls[0]).SelectedItem.Text+" ");
}
Related
i want to make datagrid (checkecbox column)rows ticked.those rows in datagrid that match studentcode colum already in listbox items .
i have tried the code below.but i don't get the proper result
private void btnConvertItemsToCheckedRows_Click(object sender, EventArgs e)
{
if( ListBox1.ListBox.Items.Count>0)
{
for (int i = 0; i <ListBox1.ListBox.Items.Count; i++)
{
foreach (DataGridViewRow row in GridStudents.DataGridView.Rows)
{
if(row.Cells["StudentCode"].Value.ToString().Equals(ListBox1.ListBox.Items[i]))
{
GridStudents.DataGridView.Rows[i].DataGridView["ChekboxColumn", i].Value = true;
}
}
}
}
}
Try this instead :
private void btnConvertItemsToCheckedRows_Click(object sender, EventArgs e)
{
if( ListBox1.ListBox.Items.Count>0)
{
for (int i = 0; i <ListBox1.ListBox.Items.Count; i++)
{
foreach (DataGridViewRow row in GridStudents.DataGridView.Rows)
{
if(row.Cells["StudentCode"].Value.ToString().Equals(ListBox1.ListBox.Items[i]))
{
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells["ChekboxColumn"];
chk.TrueValue = true;
chk.Value = chk.TrueValue;
}
}
}
GridStudents.EndEdit();
}
}
I'm trying to get a list of strings from the database.
For each string in the list i want to add a label & textbox to the page.
On button submit I want to collect the textbox value as well as the corresponding label value then save it to the database.
I need help retrieving the values from the textboxes.
What I have so far:
Panel1 is on the aspx page
protected List<string> items = MyClass.GetItems();
protected void Page_Load(object sender, EventArgs e)
{
GenerateItemsTable();
}
private void GenerateItemsTable()
{
Table table = new Table();
table.ID = "Table1";
//PlaceHolder1.Controls.Add(table);
Panel1.Controls.Add(table);
foreach (var x in items)
{
TableRow row = new TableRow();
for (int y = 0; y < 1; y++)
{
TableCell labelCell = new TableCell();
labelCell.Controls.Add(CreateLabel(x));
labelCell.CssClass = "tdLabel";
row.Cells.Add(labelCell);
TableCell txbCell = new TableCell();
txbCell.Controls.Add(CreateRadNumericTextBox(x));
txbCell.Width = 30;
row.Cells.Add(txbCell);
TableCell dataTypeCell = new TableCell();
dataTypeCell.Text = "<span style='font-size: 10px; color: #777'>(student count)</span>";
dataTypeCell.Width = 100;
row.Cells.Add(dataTypeCell);
TableCell fourthCell = new TableCell();
if (x == items[items.Count - 1])
{
RadButton rb = new RadButton();
rb.ID = "submit";
rb.Text = "Submit Guidance";
rb.Skin = "Forest";
rb.Click += new EventHandler(submit_Click);
rb.AutoPostBack = true;
fourthCell.Controls.Add(rb);
row.Cells.Add(fourthCell);
}
else
{
row.Cells.Add(fourthCell);
}
}
table.Rows.Add(row);
}
}
private RadNumericTextBox CreateRadNumericTextBox(string x)
{
RadNumericTextBox rntb = new RadNumericTextBox();
rntb.ID = x;
rntb.Width = 40;
return rntb;
}
private Label CreateLabel(string x)
{
Label l = new Label();
l.ID = "label_" + x;
l.Text = "<label>" + x + "</label>";
return l;
}
protected void submit_Click(object sender, EventArgs e)
{
foreach (Control x in FindControl("Panel1").FindControl("Table1").Controls)
{
if (x is RadNumericTextBox)
{
//how to get the data??????/
}
}
}
(thanks to those that actually read the whole post)
-----------------updated solution--------------------------------------------
I decided to change it and store the list from the db at page_load. With the list stored i loop through the list and use FindControl() to access the textboxes. Something like this..
//a couple containers
protected class ItemVal
{
public int Value { get; set; }
public string Name { get; set; }
}
protected List<ItemVal> items = new List<ItemVal>();
//get the list from that database
protected void GetItems()
{
foreach (var x in MyClass.GetItems())
{
ItemVal i = new ItemVal();
i.Name = x;
items.Add(i);
}
}
//submit
protected void submit_Click(object sender, EventArgs e)
{
foreach (var x in items)
{
RadNumericTextBox rntb = FindControl(x.Name) as RadNumericTextBox;
x.Value = (int)rntb.Value;
}
}
You need to cast x to a RadNumericTextBox and then pull out the property values you want, like this:
RadNumericTextBox theRadNumericTextBox = x as RadNumericTextBox;
string val = theRadNumericTextBox.Text;
Then for the other controls you want, you will need to put if conditions for their types, like this:
if (x is Label)
{
Label theLabel = x as Label;
string valLabel = theLabel.Text;
}
Here is the full code for the method:
protected void submit_Click(object sender, EventArgs e)
{
foreach (Control x in FindControl("Panel1").FindControl("Table1").Controls)
{
Label theLabel;
RadNumericTextBox theRadNumericTextBox;
if (x is RadNumericTextBox)
{
RadNumericTextBox theRadNumericTextBox = x as RadNumericTextBox;
string val = theRadNumericTextBox.Text;
}
if (x is Label)
{
Label theLabel = x as Label;
string valLabel = theLabel.Text;
}
// Either store up in a list or save to the database on each loop; it is recommended to store a list and send all the changes at once for a database save, but that is your choice
}
}
The thing I want to do is that i want to select 1,2 or 3 items from the listbox and save them into a session and then display them all on another form in a listbox.
Here's my code!
This is my first post on stack overflow, so no hate please <3
//WebForm1
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lstProducts.Items.Add("Soap");
lstProducts.Items.Add("Schampoo");
lstProducts.Items.Add("Conditioner");
}
}
protected void cmdBuy_Click(object sender, EventArgs e)
{
string[] products = new string[3];
for (int i = 0; i < lstProducts.Items.Count; ++i)
{
if (lstProducts.Items[i].Selected)
products[i] = lstProducts.Items[i].Text;
else
products[i] = "0";
}
Session["Cart"] = products;
}
protected void cmdCart_Click(object sender, EventArgs e)
{
if (Session["Cart"] != null)
{
Response.Redirect("WebForm2.aspx");
}
}
}
//WebForm2
protected void Page_Load(object sender, EventArgs e)
{
string[] products = (string[])Session["Cart"];
for (int i = 0; i < 3; ++i)
{
if (products[i] != "0")
{
lstCart.Items.Add(products[i]);
}
}
}
}
}
The thing is that I only get the last selected item to display in the listbox on form2???
Try this
To store all items of the of the list box, you can add that items in array as:
string[] a = new string[]{"item 1","item 2","item 3"};
Session["values"] = a;
And in the next page, you can retrieve it like this.
string[] a = (string[])Session["values"]
EDIT #1
your case you can do like
ArrayList al = new ArrayList();
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected == true)
{
al.Add(ListBox1.Items[i].Value);
}
}
Session["selectedValues"] = al;
now you can use this sessiom variable in another page, but don't forget to cast in ArrayList type of object.
I will have a treenode with some nodes. I will have a datagridview on my form. Initially i will load some data in to the gridview. Now if i select a node at my trreview i would like to make a particular row as selected one.
Suppose my treeview is as follows
Root
|-> Child
|->Child1
If i select child i would like to make a corresponding row as selected if child1 another row should get selected.
Any idea please
1) you need to map the nodes to corresponding datagrid rows
this.dataGridView1.Rows[0].Tag = id; // a node id
2) handle node click event and find corresponding row by id and select it
if (tvwACH.SelectedNode.Parent != null)
{
int id = (int)tvwACH.SelectedNode.Tag ; // make sure you've already assigned tag when creating Three nodes and data rows
foreach(DataGridViewRow row in this.dataGridView1.Rows)
{
int rowId = (int)row.Tag ;
if(rowId == id)
{
row.Selected = ture;
}
else
{
row.Selected = false; //discard other rows
}
}
}
yourDataGridView.Rows(nRowIndex).Selected = true;
This is the code i have written
private void tvwACH_AfterSelect(object sender, TreeViewEventArgs e)
{
string node = string.Empty;
if (tvwACH.SelectedNode.Parent != null)
{
node = tvwACH.SelectedNode.Text.ToString();
if (node == "FileHeader")
{
int tag = Convert.ToInt16(tvwACH.SelectedNode.Tag.ToString());
this.dataGridView1.Rows[0].Tag = tag;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
int rowId = (int)row.Tag;
if (rowId == tag)
{
row.Selected = true;
}
}
}
string strSwitch = tvwACH.SelectedNode.Parent.Text;
switch (strSwitch)
{
case "ACH":
{
dataGridView1.Visible = true;
dataGridView1.Rows.Clear();
node = tvwACH.SelectedNode.Text;
StreamReader sr = new StreamReader(node);
while (sr.Peek() >= 0)
{
string line = sr.ReadLine();
dataGridView1.Rows.Add(rectype[line.Substring(0, 1)].ToString(), line);
}
sr.Close();
}
break;
}
}
}
Try this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int flage = 1;
private void button1_Click(object sender, EventArgs e)
{
flage = 1;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Selected)
{
DataGridViewRow currentRow = dataGridView1.SelectedRows[0];
TreeNode node1 = new TreeNode(currentRow.Cells[1].Value.ToString());
TreeNode node2 = new TreeNode(currentRow.Cells[2].Value.ToString());
TreeNode node3 = new TreeNode(currentRow.Cells[3].Value.ToString());
TreeNode[] TreeArray = new TreeNode[] { node1,node2, node3 };
TreeNode finalnode = new TreeNode(currentRow.Cells[0].Value.ToString(), TreeArray);
treeView1.Nodes.Add(finalnode);
flage = 1;
break;
}
else
{
flage = 0;
}
}
if(flage==0)
{
MessageBox.Show("Row is not Selected Please select the row");
}
}
private void button2_Click(object sender, EventArgs e)
{
treeView1.Nodes.Remove( treeView1.SelectedNode);
}
int flage2;
private void button3_Click(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Selected)
{
dataGridView1.Rows.Remove(dataGridView1.SelectedRows[0]);
flage2 = 1;
break;
}
else
{
flage2 = 0;
}
}
if (flage2 == 0)
{
MessageBox.Show("Row is not selected Please select the row");
}
}
}
I want to find the column name of the cell in the below event of a datagridview.
protected void grvDetailedStatus_ItemDataBound(object sender, DataGridItemEventArgs e)
{
for (int i = 0; i <= e.Item.Cells.Count - 1; i++)
{
System.DateTime cellDate = default(System.DateTime);
if (System.DateTime.TryParse(e.Item.Cells[i].Text, out cellDate))
{
e.Item.Cells[i].Text = string.Format("{0:d}", cellDate);
}
}
}
Is there any way to find the column name of the cell I am manipulating?
EDIT: Sorry for not giving a clear explanation. Let me explain it more clearly.
I want to do the below formatting only for particular column values:
protected void grvDetailedStatus_ItemDataBound(object sender, DataGridItemEventArgs e)
{
for (int i = 0; i <= e.Item.Cells.Count - 1; i++)
{
System.DateTime cellDate = default(System.DateTime);
if (System.DateTime.TryParse(e.Item.Cells[i].Text, out cellDate))
{
e.Item.Cells[i].Text = string.Format("{0:d}", cellDate);
}
}
}
Say, for example, I have to change the format of the date only for the "column1" and "column5". So now I want to know the column name and with that I want to format that column alone and leave the rest.
protected void grvDetailedStatus_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if ( columnName == "Column1")
{
for (int i = 0; i <= e.Item.Cells.Count - 1; i++)
{
System.DateTime cellDate = default(System.DateTime);
if (System.DateTime.TryParse(e.Item.Cells[i].Text, out cellDate))
{
e.Item.Cells[i].Text = string.Format("{0:d}", cellDate);
}
}
}
}
.
I'll assume you're using a DataGrid. GridView is a similar control, but slightly different flavor. I'll also assume you're using autogenerated columns, in which case the DataGrid.Columns collection won't help you.
Instead of checking the column names each time, it's better to store the indexes of the columns you're interested in once. Like this:
private List<int> _myColumns;
protected void grvDetailedStatus_ItemDataBound(object sender,
DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
_myColumns = new List<int>();
for (int i = 0; i < _columnNames.Length; i++)
{
switch (e.Item.Cells[i].Text)
{
case "column1":
case "column5":
// Interesting column, store index
_myColumns.Add(i);
break;
}
}
}
else if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
foreach (int i in _myColumns)
{
// Your original code:
System.DateTime cellDate = default(System.DateTime);
if (System.DateTime.TryParse(e.Item.Cells[i].Text, out cellDate))
{
e.Item.Cells[i].Text = string.Format("{0:d}", cellDate);
}
}
}
}
If you really wanted to store all the column names, it would be fairly easy to adapt this code (or look at an earlier version of this post).
If you're running through the individual cells during the ItemDataBound event and need to get the column name of a cell then you can use the following:
protected void myDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
int i = 0;
string colName;
foreach (TableCell cell in e.Item.Cells)
{
//string cellTxt = e.Item.Cells[i].Text;
DataRowView dv = (DataRowView)e.Item.DataItem;
colName = dv.DataView.Table.Columns[i].ColumnName;
i++;
}
}
First you need to check whether the e.Item is an ListItemType.Item or ListItemType.AlternatingItem or ListItemType.Header. If it's a Header, then you can get the title as follows:
protected void grvDetailedStatus_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Header))
{
// retrieve the header text... e.g.
string FirstCellHeader = e.Item.Cells[0].Text;
}
else if((e.Item.ItemType == ListItemType.Item) ||
(e.Item.ItemType == ListItemType.AlternatingItem))
{
// your code for items
}
}