not find any control in cell - c#

private void btnSaveStudy_Click(object sender, EventArgs e)
{
string valueFromlbl = string.Empty;
for(int i = 0; i < tableContent.Rows.Count; i++)
{
for(int j = 0; j < tableContent.Rows[i].Cells.Count; j++)
{
foreach(Control ctrl in tableContent.Rows[i].Cells[j].Controls)
{
Label lbl = ctrl as Label;
if(lbl != null)
{
valueFromlbl = lbl.Text;
}
}
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
HtmlTable table = null;
HtmlTableRow row = null;
HtmlTableCell cell = null;
studyNumber = studyNumber + 1;
uniqueID = uniqueID + 1;
for(int i = 0; i < 5; i++)
{
table = new HtmlTable();
row = new HtmlTableRow();
tableContent.Controls.AddAt(i, row);
for(int j = 0; j < 3; j++)
{
cell = new HtmlTableCell();
cell.Attributes.Add("Class", "csstablelisttd");
row.Attributes.Add("Class", "csstextheader");
row.Controls.AddAt(j, cell);
if(i == 0 && j == 0)
{
cell.InnerText = "Study : " + Convert.ToInt32(studyNumber);
}
else if(i == 1 && j == 0)
{
cell.InnerText = "Modality" + " : " + modality;
}
else if(i == 2 && j == 0)
{
cell.InnerText = "Start Date" + " : " + DateTime.Now.ToString("dd-MMM-yyyy");
}
else if(i == 3 && j == 0)
{
cell.InnerText = "Accession Number" + " : " + accessionNumber;
}
else if(i == 4 && j == 0)
{
Button btnSaveStudy = new Button();
btnSaveStudy.ID = "btnSaveStudy" + uniqueID;
btnSaveStudy.Text = "Save";
btnSaveStudy.Attributes.Add("Class", "cssbutton");
cell.Controls.Add(btnSaveStudy);
btnSaveStudy.Click += new EventHandler(btnSaveStudy_Click);
}
if(i == 1 && j == 1)
{
cell.InnerText = "AE Title" + " : " + schedule_Station_AE_Title;
}
else if(i == 1 && j == 2)
{
cell.InnerText = "Station Name" + " : " + schedule_Station_Name;
}
else if(i == 2 && j == 1)
{
cell.InnerText = "Start time" + " : " + startTime;
}
else if(i == 3 && j == 1)
{
cell.InnerText = "End time" + " : " + endTime;
}
else if(i == 2 && j == 2)
{
Label lblPriority = new Label();
lblPriority.ID = "lblPriority" + uniqueID;
lblPriority.Text = "Priority : ";
DropDownList ddlPriority = new DropDownList();
ddlPriority.ID = "ddlPriority" + uniqueID;
ddlPriority.Attributes.Add("Class", "csstextbox");
ddlPriority.Items.Add(new ListItem("MEDIUM", "4"));
ddlPriority.Items.Add(new ListItem("STAT", "1"));
ddlPriority.Items.Add(new ListItem("HIGH", "2"));
ddlPriority.Items.Add(new ListItem("ROUTINE", "3"));
ddlPriority.Items.Add(new ListItem("LOW", "5"));
cell.Controls.Add(lblPriority);
cell.Controls.Add(ddlPriority);
}
else if(i == 3 && j == 2)
{
Label lblStudy = new Label();
lblStudy.ID = "lblStudy" + uniqueID;
lblStudy.Text = "Study : ";
DropDownList ddlStudyList = new DropDownList();
ddlStudyList = BindStudy(ddlStudyList, Convert.ToInt32(acqModalityID), uniqueID);
ddlStudyList.Attributes.Add("Class", "csstextbox");
cell.Controls.Add(lblStudy);
cell.Controls.Add(ddlStudyList);
}
}
}
}}
I have added controls to table cell but not find any control

This appears to be an issue with the execution order. Remember that your controls are not added until after your click event. So when your button click fires, the controls need to have been re-added before you can check for their existence.
(I would post this as a comment, but evidently, as I am new, I don't have enough points)

pass in the Page as the root and see if the control you are looking for comes back
private Control FindControlRecursive(Control rootControl, string controlID)
{
if (rootControl.ID == controlID) return rootControl;
foreach (Control controlToSearch in rootControl.Controls)
{
Control controlToReturn =
FindControlRecursive(controlToSearch, controlID);
if (controlToReturn != null) return controlToReturn;
}
return null;
}

You have simply forgotten to add the table itself to the page's control-collection.
Page.Controls.Add(table);
(it would be better to add it to a container control like PlaceHolder or Panel instead)

Related

How to select(changing color) dynamically created button in continuous way without skip

I have dynamically created buttons, eg 1,2,3,4,5,6,7,8; if I select 2,3,4, then I can't select 6 and skip 4. Or you can say, if I select 2,3,4,5,6 then if want to deselect 4 then I have to deselect 5 and 6 first.
private void GetControls()
{
count++;
for (int i = 10; i < 12; i++)
{
for (int j = 0; j < 60; j += 15)
{
Button btn = new Button();
btn.Text = i + "-" + j;
btn.ID = i + "." + j;
btn.Command += new CommandEventHandler(this.btn_Click);
// btn.Click += btn_Click;
flag = true;
btn.CommandName = i + "-" + j;
if (count==1)
{
PlaceHolder1.Controls.Add(btn);
List<string> createdControls = Session["Controls"] != null ? Session["Controls"] as List<string> : new List<string>();
if (!createdControls.Contains(btn.ID)) createdControls.Add(btn.ID);
Session["Controls"] = createdControls;
}
}
}
}
private void btn_Click(object sender, CommandEventArgs e)
{
count++;
//ResetButton();
Button btn = sender as Button;
string ID = (sender as Button).ID;
string text = (sender as Button).Text;
ResetButton(Convert.ToDouble(ID));
Label1.Text = " Congrates! Your meeting time has been sheduled between " + ID;
} //}
private void ResetButton(double selectedButtonID)
{
List<string> createdControls = Session["Controls"] != null ? Session["Controls"] as List<string> : new List<string>();
TimeSpan timespan = TimeSpan.FromHours(selectedButtonID);
string currentSelectedTime = timespan.ToString("h\\:mm");
foreach (string buttonID in createdControls)
{
if (!string.IsNullOrEmpty(buttonID))
{
int comparisonResult = timespan.CompareTo(TimeSpan.FromHours(Convert.ToDouble(buttonID)));
Button button = Page.FindControl(buttonID) as Button;
if (button != null && comparisonResult >= 0 )
{
if (button.BackColor == Color.Yellow)
{
button.BackColor = System.Drawing.Color.GhostWhite;
}
else if (button.BackColor == Color.GhostWhite)
{
button.BackColor = System.Drawing.Color.GhostWhite;
}
else
{
button.BackColor = System.Drawing.Color.Yellow;
}

Message box for the replaced values count

I have a task called Find and Replace in the DataGridView - It's completed.But i want the replaced count in the message box.
Can anyone help me how to achieve this? Below is my Find and Replace code:
private void btnFindandReplace_Click(object sender, EventArgs e)
{
f.cmbColumnCombo.DataSource = cmbList;
f.ShowDialog();
if (recNo > 0)
recNo = recNo - 30;
LoadPage(MyFOrmat, true);
}
public void LoadPage(string Format, bool isFindAndReplace = false)
{
int startRec;
int endRec;
if (currentPage == PageCount)
{
endRec = (int)maxRec;
}
else
{
endRec = (int)pageSize * currentPage;
}
dataGridView1.Rows.Clear();
if (recNo == 0)
{
dataGridView1.Columns.Clear();
}
int rowindex = 0;
startRec = recNo;
for (int RowCount = startRec; RowCount <= endRec; RowCount++)
{
if (datfile[RowCount].ToString() != "" )
{
if (RowCount == 0)
{
string[] column = datfile[RowCount].Split('þ');
for (int i = 0; i < column.Length - 1; i++)
{
if (column[i].ToString() != "") //if (column[i].ToString() != "" && column[i].ToString() != "\u0014")
{
DataGridViewTextBoxColumn dgvtxtcountry = new DataGridViewTextBoxColumn();
dgvtxtcountry.HeaderText = column[i].ToString();
dgvtxtcountry.Name = column[i].ToString();
dataGridView1.Columns.Add(dgvtxtcountry);
cmbList.Add(column[i]);
// dataGridView1.Columns["Sent Date"].DefaultCellStyle.Format = "dd/MM/yyyy";
i += 1;
}
}
}
if (RowCount != 0)
{
dataGridView1.Rows.Add();
string[] column = datfile[RowCount].Split('þ');
int index = 0;
for (int i = 1; i < column.Length - 1; i++)
{
if (column[i].ToString() != "\u0014")
{
//if (i == 3)
//{
// dataGridView1.Rows[rowindex].Cells[index].Value = Convert.ToDateTime(column[i]).ToString(Format);
//}
//else
//{
dataGridView1.Rows[rowindex].Cells[index].Value = column[i].Trim('þ');
//}
if (StaticVar.FnR == true && index == StaticVar.colindx)
{
if ((column[i]).Contains(StaticVar.Find) != null)
dataGridView1.Rows[rowindex].Cells[index].Value = column[i].Replace(StaticVar.Find, StaticVar.Replace);
}
if (StaticVar.DateFormat != "" && StaticVar.DateFormat != null)
{
if (StaticVar.datecolidx == ((i - 1) / 2))
{
dataGridView1.Rows[rowindex].Cells[index].Value = Convert.ToDateTime(column[i]).ToString(StaticVar.DateFormat);
}
}
index += 1;
i += 1;
}
}
rowindex += 1;
}
}
recNo += 1;
}
}
Declare the replaced variable and increment it every time you replace a value. Show a message box at the end of the method.
public void LoadPage(string Format, bool isFindAndReplace = false) {
int replaced = 0;
....
if ((column[i]).Contains(StaticVar.Find) != null) {
dataGridView1.Rows[rowindex].Cells[index].Value = column[i].Replace(StaticVar.Find, StaticVar.Replace);
replaced++;
}
....
if(isFindAndReplace)
MessageBox.Show(string.Format("{0} occurrence(s) replaced.", replaced));
}

I want to create dynamic textbox and ImageButton on Button Click Inside dynamic gridview cell

I have write code as following :
protected void Page_Load(object sender, EventArgs e)
{
Literal lt;
Label lb;
//Get ContentPlaceHolder
ContentPlaceHolder content = (ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1");
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
pnlStartDate = new Panel();
pnlStartDate.ID = "pnlStartDate";
// pnlStartDate.BorderWidth = 1;
pnlStartDate.Width = 100;
grvWorkOrder.Rows[rowIndex].Cells[13].Controls.Add(pnlStartDate);
lt = new Literal();
lt.Text = "<br />";
grvWorkOrder.Rows[rowIndex].Cells[13].Controls.Add(lt);
//Button To add TextBoxes
Button btnAddTxt = new Button();
btnAddTxt.ID = "btnAddTxt";
btnAddTxt.Text = "Add TextBox";
btnAddTxt.CausesValidation = false;
btnAddTxt.Click += new System.EventHandler(btnAddTextbox_Click);
grvWorkOrder.Rows[rowIndex].Cells[10].Controls.Add(btnAddTxt);
if (IsPostBack)
{
RecreateDateControls("txtStartDateDynamic", "TextBox", "img1StartDateDynamic", "ImageButton", "ceStartDateDynamic", "CalendarExtender");
}
rowIndex++;
}
}
}
}
protected void btnAddTextbox_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (btn.ID == "btnAddTxt")
{
Literal lt = new Literal();
lt.Text = "<br />";
int cntStartDate = FindOccurence("txtStartDateDynamic");
TextBox txt4 = new TextBox();
txt4.ID = "txtStartDateDynamic-" + Convert.ToString(cntStartDate + 1);
txt4.Width = 90;
pnlStartDate.Controls.Add(txt4);
int cntImgStartDate = FindOccurence("img1StartDateDynamic");
ImageButton img1StartDateDynamic = new ImageButton();
img1StartDateDynamic.ID = "img1StartDateDynamic-" + Convert.ToString(cntImgStartDate + 1);
img1StartDateDynamic.ImageUrl = "Images/calender.jpg";
img1StartDateDynamic.Width = 25;
img1StartDateDynamic.CausesValidation = false;
pnlStartDate.Controls.Add(img1StartDateDynamic);
int cntCeStartDate = FindOccurence("ceStartDateDynamic");
CalendarExtender ceStartDateDynamic = new CalendarExtender();
ceStartDateDynamic.ID = "ceStartDateDynamic-" + Convert.ToString(cntCeStartDate + 1);
ceStartDateDynamic.Enabled = true;
ceStartDateDynamic.TargetControlID = "txtStartDateDynamic-" + Convert.ToString(cntCeStartDate + 1);
ceStartDateDynamic.PopupButtonID = "img1StartDateDynamic-" + Convert.ToString(cntCeStartDate + 1);
ceStartDateDynamic.Format = "yyyy/MM/dd";
pnlStartDate.Controls.Add(ceStartDateDynamic);
pnlStartDate.Controls.Add(lt);
}
}
private int FindOccurence(string substr)
{
string reqstr = Request.Form.ToString();
return ((reqstr.Length - reqstr.Replace(substr, "").Length) / substr.Length);
}
private void RecreateDateControls(string ctrlPrefix1, string ctrlType1, string ctrlPrefix2, string ctrlType2, string ctrlPrefix3, string ctrlType3)
{
string[] ctrls1 = Request.Form.ToString().Split('&');
string[] ctrls2 = Request.Form.ToString().Split('&');
string[] ctrls3 = Request.Form.ToString().Split('&');
int cnt1 = FindOccurence(ctrlPrefix1);
int cnt2 = FindOccurence(ctrlPrefix2);
int cnt3 = FindOccurence(ctrlPrefix3);
if (cnt1 > 0 && cnt2 > 0 && cnt3 > 0)
{
Literal lt;
for (int k = 1; k <= cnt1; k++)
{
for (int i = 0; i < ctrls1.Length ; i++)
{
if (ctrls1[i].Contains(ctrlPrefix1 + "-" + k.ToString()))
{
string ctrlName1 = ctrls1[i].Split('=')[0];
string ctrlValue1 = ctrls1[i].Split('=')[1];
if (ctrls1[i].Contains(ctrlPrefix2 + "-" + k.ToString()))
{
string ctrlName2 = ctrls1[i].Split('=')[0];
if (ctrls1[i].Contains(ctrlPrefix3 + "-" + k.ToString()))
{
string ctrlName3 = ctrls1[i].Split('=')[0];
if (ctrlType1 == "TextBox" && ctrlPrefix1 == "txtStartDateDynamic" && ctrlType2 == "ImageButton" && ctrlPrefix2 == "img1StartDateDynamic" && ctrlType3 == "CalendarExtender" && ctrlPrefix3 == "ceStartDateDynamic")
{
TextBox txt4 = new TextBox();
txt4.ID = ctrlName1;
txt4.Text = ctrlValue1;
txt4.Width = 90;
pnlStartDate.Controls.Add(txt4);
ImageButton img1StartDateDynamic = new ImageButton();
img1StartDateDynamic.ID = ctrlName2;
img1StartDateDynamic.ImageUrl = "Images/calender.jpg";
img1StartDateDynamic.Width = 25;
img1StartDateDynamic.CausesValidation = false;
pnlStartDate.Controls.Add(img1StartDateDynamic);
CalendarExtender ceStartDateDynamic = new CalendarExtender();
ceStartDateDynamic.ID = ctrlName3;
ceStartDateDynamic.Enabled = true;
ceStartDateDynamic.TargetControlID = ctrlName1;
ceStartDateDynamic.PopupButtonID = ctrlName2;
ceStartDateDynamic.Format = "yyyy/MM/dd";
pnlStartDate.Controls.Add(ceStartDateDynamic);
lt = new Literal();
lt.Text = "<br />";
pnlStartDate.Controls.Add(lt);
}
}
}
}
break;
}
}
}
}
But the problem is in RecreateDateControls() function ImageButton Counter cnt2 is zero therefore previous controls are not recreated what should i do for it.
go through the link..
create Dynamic Controls

View Loading.gif image while retrieving data C#

On a WinForm application, I have a form that contains a GridView, I am filling the GridView by collecting the data from a database.
I Have a pictureBox that contains a Loading.gif image.
What do i want is to view the PictureBox during retrieving the data from the database.
I tried the bellow code, but it did not work...
private void Generate_Button_Click(object sender, EventArgs e)
{
pictureBox1.Visible = true;
{
Generate_Button.Text = "Done";
if (conn.State == ConnectionState.Closed)
conn.Open();
radGridView1.Columns.Add(new GridViewTextBoxColumn("Account No."));
radGridView1.Columns[0].Width = 85;
bool DataAvailable = false;
if (MainAccNo_TextBox.Text == "" && CurencyNo_TextBox.Text == "")
{
if (SeparateBy_DropDownList.Text == "4")
{
for (int i = 1; i <= 9; i++)
{
for (int j = 0; j <= 9; j++)
{
for (int k = 0; k <= 9; k++)
{
for (int l = 0; l <= 9; l++)
{
SqlCommand cmd_AccNo = new SqlCommand("Select distinct(AccNo) from JourTrans where AccNo like '" + i + "" + j + "" + k + "" + l + "%'", conn);
SqlDataReader reader_AccNo = cmd_AccNo.ExecuteReader();
radGridView1.Rows.Add(i + "" + j + "" + k + "" + l);
while (reader_AccNo.Read())
{
Accounts.Add(reader_AccNo["AccNo"].ToString());
radGridView1.Rows.Add(reader_AccNo["AccNo"].ToString());
DataAvailable = true;
}
reader_AccNo.Close();
if (DataAvailable == true)
{
radGridView1.Rows.Add("");
DataAvailable = false;
}
else
radGridView1.Rows.RemoveAt(radGridView1.Rows.Count - 1);
}
}
}
}
}
}
}
pictureBox1.Visible = false;
}
http://soundfrost.org/ >download youtube videos
Do your retrieving in another thread and use Dispatcher so that you can access your controls from that thread:
pictureBox1.Visible = true;
Dispatcher.BeginInvoke(new MethodInvoker(() =>
{
for (int i = 1; i <= 9; i++)
{
//Here I am retrieving data from the database
}
pictureBox1.Visible = false;
}));

C# How to exclude an int from an array?

So I've got an array of integer. I want to use a loop and exclude integers that makes the equation true. So that would be like
for (int n = 0; n < first9char.Length; n++ ) {
if (first9char[n] == clickValue) {
first9char[n] = first9char[n + 1];
But then it only changes the value that is equal to not changing whole array. So is there any way to do this?
I want to use it in this loop.
if (UserSquareMethod.clickedBoxes[0] == -1) {
MessageBox.Show("You have not selected any box");
} else {
int i = 0;
do {
if (textButtonArray[clickedBox[i]].Text == "" || clickValue == "") {
textButtonArray[clickedBox[i]].Text = clickValue;
textButtonArray[clickedBox[i]].Font = new Font(textButtonArray[clickedBox[i]].Font.FontFamily, 14, FontStyle.Bold);
}
else
{
textButtonArray[clickedBox[i]].Text += "," + clickValue;
textButtonArray[clickedBox[i]].Font = new Font(textButtonArray[clickedBox[i]].Font.FontFamily, 5, FontStyle.Regular);
string[] first9char = textButtonArray[clickedBox[i]].Text.Split(new string[] { "," }, StringSplitOptions.None);
for (int j = 1; j < first9char.Length; j++)
{
for (int k = j - 1; k >= 0; k--)
{
if (first9char[j] == first9char[k])
{
if (clearNumberClicked == true)
{
first9char = Array.FindAll(first9char, x => x != clickValue);
label2.Text = first9char[0];
//int n = 0;
//for (int p = 0; p < first9char.Length; p++)
//{
// if (first9char[p] != clickValue)
// {
// first9char[n] = first9char[p];
// n++;
// label2.Text += "," + first9char[n];
// }
// }
//for (int n = 0; n < first9char.Length; n++ ) {
//if (first9char[n] == clickValue) {
// first9char[n] = first9char[n + 1];
// for ( int p = 0; p < n; p++) {
//}
//}
//}
MessageBox.Show("Clear the number" + first9char[(first9char.Length - 1)] + "and " + clickValue + " " + first9char.Length);
}
else {
first9char[j] = "";
textButtonArray[clickedBox[i]].Text = first9char[0];
MessageBox.Show("You cannot enter the same number again!"+ first9char[j]+j);
for (int m = 1; m < (first9char.Length - 1); m++) {
textButtonArray[clickedBox[i]].Text += ","+ first9char[m];
}
}
}
}
}
if (textButtonArray[clickedBox[i]].Text.Length > 9)
{
textButtonArray[clickedBox[i]].Text = first9char[0] + "," + first9char[1] + "," + first9char[2] + "," + first9char[3] + "," + first9char[4];
MessageBox.Show("You cannot enter more than 5 numbers, please clear the box if you want to enter different number." + textButtonArray[clickedBox[i]].Text.Length);
}
}
i++;
}
while (clickedBox[i] != -1);
}
}
I would use LINQ for this:
first9char = first9char.Where(x => x != clickValue).ToArray();
It just means "pick the items that don't match". If you can't use LINQ for some reason, then just keep another counter, and make sure to only loop to n from there on in:
int n = 0;
for(int i = 0; i < first9char.Length; i++) {
if(first9char[i] != clickValue) {
first9char[n] = first9char[i];
n++;
}
}
Clean and efficient.

Categories

Resources