Can I put the button wherever the row I wanted? For example, I want to put the button in row[2], so the button will be appear in that row at every columns.
I tried to made something like this to made the button in the right, but I failed :
DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
btn.HeaderText = "Delete";
btn.Text = " ";
btn.UseColumnTextForButtonValue = true;
tbl_incomingrawmaterialform.ColumnCount = 5;
tbl_incomingrawmaterialform.Columns[0].HeaderText = "Header1";
tbl_incomingrawmaterialform.Columns[1].HeaderText = "Header2";
tbl_incomingrawmaterialform.Columns[2].HeaderText = "Header3";
tbl_incomingrawmaterialform.Columns[3].HeaderText = "Header4";
tbl_incomingrawmaterialform.Columns.Add(btn);
And this is for the looping data :
tbl_incomingrawmaterialform.Rows[i].Cells[0].Value = (i + 1);
tbl_incomingrawmaterialform.Rows[i].Cells[1].Value = Value1;
tbl_incomingrawmaterialform.Rows[i].Cells[2].Value = Value2;
tbl_incomingrawmaterialform.Rows[i].Cells[3].Value = Value3;
DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
tbl_incomingrawmaterialform.Rows.Add(btn);
i++;
But this doesn't work. I thought if I empty one columns, the button will automatically be there. I'm sorry, I'm still beginner.
You should use gridView.Columns.Insert() instead of gridView.Rows.Add(). Please take a look:
int columnIndex = 4;
if (tbl_incomingrawmaterialform.Columns["Delete"] == null)
{
tbl_incomingrawmaterialform.Columns.Insert(columnIndex, btn);
}
Happy coding!
Related
I want to add 2 label at the same group box, this is my code :
int x = 0;
foreach (var item in comboboxinterface.Items)
{
drv = item as DataRowView;
Button btn = new Button();
Label lblerrortoday = new Label();
Label lblcounterror = new Label();
btn.Text = drv.Row.ItemArray[0].ToString();
btn.Location = new System.Drawing.Point(10, 20 + (x * 30));
lblcounterror.Location = new System.Drawing.Point(100, 25 + (x * 30));
lblcounterror.Text = "No";
lblerrortoday.Location = new System.Drawing.Point(120, 25 + (x * 30));
lblerrortoday.Text = "Error Today";
grouptodayerror.Controls.Add(btn);
grouptodayerror.Controls.Add(lblcounterror);
grouptodayerror.Controls.Add(lblerrortoday);
x++;
}
But when i start the program, the lblerrortoday is not showing up but the lblcountererror is fine, when i tried to comment the the lblcounterror, the lblerrortoday is showing fine, did i miss something?
For me your approach seems to ok, only point to note that width of labels that may cause for not displaying other label.
You could use Control.AutoSize property , This may resolve your problem
lblcounterror.AutoSize = true;
lblerrortoday.AutoSize = true;
The TextBox is there, but overlapped by lblcounterror. Reduce Width of lblcounterror and you will see lblerrortoday.
I am new to C# and I am using windows forms. as shown in screenshot I have cell in DataGridView which contains semicolon separated values. I have this code which creates new buttons but I am missing the part where I can assign each value in the cell to a new button.
I want to split those values (HOUSENO column) and assign each one of them to a button text on my form. Anyone knows how can I do that. Thank you
// I am trying to loop all those values and assign them to a new button text but I have no idea how to do it:
{
Button btn = new Button();
btn.Text =
btn.Width = 162;
btn.Height = 100;
flowLayoutPanel1.Controls.Add(btn);
}
Assuming you're using a DataSet/DataTable, you haven't specified:
var row = dt.Rows[0]["HOUSENO"].ToString(); // no idea if you're returning more than one row
string[] houseNumbers = row.Split(';');
foreach(string houseNum in houseNumbers)
{
Button btn = new Button();
btn.Text = houseNum;
btn.Width = 162;
btn.Height = 100;
flowLayoutPanel1.Controls.Add(btn);
}
You can use Split method on string.
Simple example:
string test = "17;25;85;24;54;32";
string[] results = test.Split(';');
foreach(string result in results)
{
// here add buttons
}
populate grid view and then on cell click i populate text box on cell click event like,
private void dgvCompany_CellClick(object sender, DataGridViewCellEventArgs e)
{
int i = dgvCompany.SelectedCells[0].RowIndex;
ID = dgvCompany.Rows[i].Cells[0].Value.ToString();
txtCompanyName.Text = dgvCompany.Rows[i].Cells[1].Value.ToString();
txtContactName.Text = dgvCompany.Rows[i].Cells[2].Value.ToString();
txtContactPersonPhone.Text = dgvCompany.Rows[i].Cells[3].Value.ToString();
txtAddress.Text = dgvCompany.Rows[i].Cells[4].Value.ToString();
txtCity.Text = dgvCompany.Rows[i].Cells[5].Value.ToString();
txtPhone.Text = dgvCompany.Rows[i].Cells[6].Value.ToString();
cbIsActive.Checked = Convert.ToBoolean(dgvCompany.Rows[i].Cells[7].Value);
}
here ID is string variable after populating text box i edit and save update and then refill grid as previous method like,
public void FillCompanyInfo()
{
DataTable dtCompanyInfo = objFunctions.GetCompanyInfo();
if(dtCompanyInfo.Rows.Count>0)
{
dgvCompany.DataSource = dtCompanyInfo;
if (this.dgvCompany.Columns.Count == 8)
{
DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();
checkColumn.Name = "";
checkColumn.HeaderText = "Select";
checkColumn.Width = 50;
checkColumn.ReadOnly = false;
checkColumn.FillWeight = 10; //if the datagridview is resized (on form resize) the checkbox won't take up too much; value is relative to the other columns' fill values\\
dgvCompany.Columns.Add(checkColumn);
}
}
}
here i use if condition counting column to 8 because each time it add column Select and after populating grid again when i click on grid and cell click event fire result in error ,
on line ,
ID = dgvCompany.Rows[i].Cells[0].Value.ToString();
object reference not found because it is getting null on cell 0 while it was working fine before update on cell click and when i use break point i am getting ID on cells[1] and cell[0] is null
i am not understanding this error
Hopes for your suggestion
Thanks
I have three DropdownList named
drpUniversity
drpFaculty
drpSupervisor
in my page.
In drpUniversity has a list of University and in last index has Others if the listed Universities are not sufficient for users.
Like as:
American University,
George Washington University,
Florida Hospital College of Health Sciences
and Others
Now, in the drpUniversity_SelectedIndexChange event I have add a Label and a TextBox when the user select the Others (or last index).
My Code:
protected void drpUniversity_SelectedIndexChanged(object sender, EventArgs e)
{
if (IsPostBack)
{
if (drpUniversity.SelectedItem.Value.ToString() == "Others".ToString())
{
int rowcount = mytable.Rows.Count;
HtmlTableRow row = new HtmlTableRow();
row.ID = "tbl_row" + (rowcount + 1);
HtmlTableCell cell1 = new HtmlTableCell();
cell1.ID = "tbl_cell1" + (rowcount + 1);
HtmlTableCell cell2 = new HtmlTableCell();
cell2.ID = "tbl_cell2" + (rowcount + 1);
cell1.Attributes["class"] = "contact";
cell2.Attributes["class"] = "contact";
TextBox tb = new TextBox();
tb.ID = "tbQty" + (rowcount + 1);
tb.Width = 276;
Label lblotherUniver = new Label();
lblotherUniver.ID = "lbluniversity";
lblotherUniver.Text = "University Name";
cell2.Controls.Add(lblotherUniver);
cell1.Controls.Add(tb);
row.Cells.Add(cell2);
row.Cells.Add(cell1);
mytable.Rows.Insert(8, row);
mytable.DataBind();
}
}
}
But, the problem is when its creating a TextBox and a Lable then the other DropDownList named drpFaculty's and drpSupervisor's SelectedIndexChange events are not working. In my drpSupervisor SelectedIndexChange event have This Code:
protected void drpSupervisor_SelectedIndexChanged(object sender, EventArgs e)
{
txtSupervisorEmail.Text = drpSupervisor.SelectedItem.Value.ToString();
txtSupervisorEmail.Visible = true;
}
This is not working after Select Others from drpUniversity. Otherwise this is working. Please help me to solve this problem.
Seems there is a javascript error in your code. Please check javascript error console in broswer.
I am working on a .Net Project and I have some problems with GridView.
I have several buttons in my page. When I click the button which binds my GridView everything is OK, but after that, when I click on some other button there appears one more cell, which is empty, at the end of Rows which contains the text "Monday".
Can someone tell me what the problem is?
Thanks
This is my code.
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.Cells[0].Text.Contains("Monday"))
{
e.Row.BackColor = Color.Gray;
e.Row.ForeColor = Color.White;
e.Row.Cells[0].ColumnSpan = 2;
e.Row.Cells.Remove(e.Row.Cells[1]);
e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Center;
e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Center;
e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Center;
e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Center;
}
else
{
e.Row.Cells[0].Width = 45;
e.Row.Cells[1].Width = 400;
e.Row.Cells[2].Width = 40;
e.Row.Cells[3].Width = 40;
e.Row.Cells[4].Width = 40;
e.Row.Cells[5].Width = 40;
e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Center;
e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Center;
e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Center;
e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Center;
e.Row.Cells[5].HorizontalAlign = HorizontalAlign.Center;
}
}
Reproduced the issue. It appears the gridview adds cells in order to have the same number as columns. The only fix I could find was adding the following code in the Page_Load event (you can also add it to any button method - but that's not practical):
foreach (GridViewRow gridRow in GridView1.Rows) {
if (gridRow.Cells[0].Text.Contains("Monday") && gridRow.Cells[0].ColumnSpan == 2 && gridRow.Cells.Count == 6)
gridRow.Cells.RemoveAt(5);
}
Another solution (with jquery) I found googling but have not tested is here:
http://forums.asp.net/t/1413833.aspx/1
Hope it helps!