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!
Related
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!
I have 5 GridViews on a page. All of them show different table data, and show calculation of totals in footer.
But I need calculate all data from all GridViews and show it in a Label.
[]
use OnRow Created..
Declare variables at the top
double Counter;
double GrandTotalCounter;
this is sample code refer and make change in yours..
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
bool IsGrandTotalRowNeedtoAdd = false;
if ((strPreviousRowID != string.Empty) && (DataBinder.Eval(e.Row.DataItem, "city_name") == null))
{
IsGrandTotalRowNeedtoAdd = true;
intTotalIndex = 0;
}
if (IsGrandTotalRowNeedtoAdd)
{
GridView grdViewProducts = (GridView)sender;
GridViewRow GrandTotalRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
GrandTotalRow.Font.Bold = true;
GrandTotalRow.BackColor = System.Drawing.Color.LightPink;
GrandTotalRow.ForeColor = System.Drawing.Color.White;
TableCell HeaderCell = new TableCell();
HeaderCell.Text = "Grand Total";
HeaderCell.HorizontalAlign = HorizontalAlign.Left;
HeaderCell.ColumnSpan = 3;
HeaderCell.Font.Bold = true;
HeaderCell.ForeColor = System.Drawing.Color.Red;
HeaderCell.Font.Size = 10;
GrandTotalRow.Cells.Add(HeaderCell);
// you can use how many fields you want to calculate
HeaderCell = new TableCell();
HeaderCell.Text = string.Format("{0:0.00}", GrandTotalCounter);
HeaderCell.HorizontalAlign = HorizontalAlign.Right;
HeaderCell.Font.Bold = true;
HeaderCell.ForeColor = System.Drawing.Color.Red;
HeaderCell.Font.Size = 10;
GrandTotalRow.Cells.Add(HeaderCell);
//////////////
GrandTotalRow.Cells.Add(HeaderCell);
grdViewProducts.Controls[0].Controls.AddAt(e.Row.RowIndex,
GrandTotalRow);
}}
on RowDataBound
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
strtotalID = DataBinder.Eval(e.Row.DataItem, "city_name").ToString();
double TtCounter = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "CounterAmt").ToString());
TotalCounter += TtCounter;
GrandTotalCounter += TCounter;
/// Add how much you want
}
I have created a Windows Forms Application and inserted a DataGridViewCheckBoxColumn as the first column of the grid, but I have been unsuccessful in setting the default value to "checked". I have tried to set it both in a loop after I insert the column and in the DefaultValuesNeeded event of the gridview. Is there an easier way to do this, or is there something I'm missing?
My code is as follows:
private void FillDataGrid()
{
BAFarmer baobj = new BAFarmer();
gvFarmers.AutoGenerateColumns = true;
gvFarmers.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
gvFarmers.DataSource = baobj.GetAllFarmersCol();
gvFarmers.Enabled = true;
gvFarmers.ReadOnly = false;
DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();
checkColumn.Name = "Include";
checkColumn.ValueType = typeof(Boolean);
checkColumn.HeaderText = "Include";
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
checkColumn.ThreeState = false;
checkColumn.TrueValue = true;
checkColumn.FalseValue = false;
gvFarmers.Columns.Insert(0, checkColumn);
foreach (DataGridViewRow row in gvFarmers.Rows)
{
row.Cells[checkColumn.Name].Value = true;
}
}
I was able to solve this by adding code to the form load event that loops through the check boxes and sets them at that point.
foreach (DataGridViewRow row in gvFarmers.Rows)
{
DataGridViewCheckBoxCell chk = DataGridViewCheckBoxCell)row.Cells["Select"];
chk.Value = true;
}
In ASP.NET webforms, is there a way to configure the FooterTemplate of the GridView to allow for overflow in the footer, but to also retain the above columns width like below? As it is now, any footer overflow also stretches the cells above it.
You can do it using gridview_rowcreated() event.
Just in the event allow rowspan and column with desired column and row
Modify it with your requirement.
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
GridView FooterGrid = (GridView)sender;
GridViewRow FooterRow = new GridViewRow(0, 0, DataControlRowType.Footer, DataControlRowState.Insert);
TableCell Cell_Footer = new TableCell();
Cell_Footer.Text =""; // As per your requirement
Cell_Footer.HorizontalAlign = HorizontalAlign.Center;
Cell_Footer.ColumnSpan = 2;
HeaderRow.Cells.Add(Cell_Footer);
Cell_Footer = new TableCell();
Cell_Footer.Text = ""; // As per your requirement
Cell_Footer.HorizontalAlign = HorizontalAlign.Center;
Cell_Footer.ColumnSpan = 1;
Cell_Footer.RowSpan = 2;
FooterRow.Cells.Add(Cell_Footer);
Cell_Footer = new TableCell();
Cell_Footer.Text = ""; // as per your requiremnt
Cell_Footer.HorizontalAlign = HorizontalAlign.Center;
Cell_Footer.ColumnSpan = 3;
FooterRow.Cells.Add(Cell_Footer);
GridView1.Controls[0].Controls.AddAt(0, FooterRow);
}
}
If want more explanation go through the link
http://codedisplay.com/merge-merging-or-split-spliting-gridview-header-row-or-columns-in-asp-net-c-vb-net/
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.