I want READONLY on text box using Boundsfield. So far enabled is working but it not carry value in rowupdating
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
var nopoject = ddlproject.SelectedValue.ToString();
Int32 curntMonth = Convert.ToInt32(DateTime.Now.ToString("MM"));
int Mont1 = curntMonth - 1;
var Comtext = "Rst" + Mont1.ToString();
GridView1.EditIndex = e.NewEditIndex;
BindData();
foreach (GridViewRow row in GridView1.Rows)
{
for (int i = 0; i < GridView1.Columns.Count; i++)
{
String headertext = GridView1.Columns[i].HeaderText;
String cellText = row.Cells[i].Text;
if (Comtext == "Rst1")
{
GridView1.Rows[e.NewEditIndex].Cells[i].Enabled = true;
}}}
Below code not working :
GridView1.Rows[e.NewEditIndex].Cells[i].Attributes.Add("readonly", "readonly");
GridView1.Rows[e.NewEditIndex].Cells[i].CssClass = "read-only";
Please advise. I want readonly on boundsfield programically when inline edit
Thank you
You can use this snippet.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
TextBox textBox = e.Row.Cells[0].Controls[0] as TextBox;
textBox.Enabled = false;
}
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == DataControlRowState.Alternate)
{
//on you condition
TextBox txt = (TextBox)e.Row.FindControl("ControlID");
if(txt !=null)
{
txt.Attributes.Add("readonly", "readonly");
}
}
}
You can also make textbox readonly in row editing event as below.
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
var nopoject = ddlproject.SelectedValue.ToString();
Int32 curntMonth = Convert.ToInt32(DateTime.Now.ToString("MM"));
int Mont1 = curntMonth - 1;
var Comtext = "Rst" + Mont1.ToString();
GridView1.EditIndex = e.NewEditIndex;
BindData();
foreach (GridViewRow row in GridView1.Rows)
{
for (int i = 0; i < GridView1.Columns.Count; i++)
{
String headertext = GridView1.Columns[i].HeaderText;
String cellText = row.Cells[i].Text;
if (Comtext == "Rst1")
{
//GridView1.Rows[e.NewEditIndex].Cells[i].Enabled = true;
TextBox tx_chdets = (TextBox)GridView1.Rows[e.NewEditIndex].FindControl(“TextBox1”);
if(tx_chdets!=null)
{
tx_chdets.Readonly=true;
}
}
}
}
}
Related
I'm trying to change cell colour on all cells with value = "1"
It is on gridview with databound columns
Looks like this
The code i have tried is this, but thos changes cells with more numbers then only "1"
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
if (e.Row.Cells[i].Text.ToLower().IndexOf("1") > -1)
{
e.Row.Cells[i].ForeColor = System.Drawing.Color.Red;
}
}
}
}
Protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
if (e.Row.Cells[i].Text.Equals("1"))
{
e.Row.Cells[i].ForeColor = System.Drawing.Color.Red;
}
}
}
}
I am setting GridView's layout features in the RowDataBound event based on the content of specific cells
private void OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[1].Text == " ")
{
e.Row.Cells[4].BackColor = System.Drawing.Color.White;
e.Row.Cells[5].BackColor = System.Drawing.Color.White;
e.Row.Cells[6].BackColor = System.Drawing.Color.White;
Is there a way to specify the range (cell index 4 to 6) where this operation has to be applied to?
Something like:
private void OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[1].Text == " " && e.Row.CellIndex >= 4 && e.Row.CellIndex <= 6)
{
e.Row.BackColor = System.Drawing.Color.White;
That should be what you need:
private void OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
for (int i = 4; i <= 6; i++)
if(e.Row.Cells[1].Text == " ")
e.Row.Cells[i].BackColor = System.Drawing.Color.White;
}
If you really need to check if its a DataControlRowType.DataRow, you can insert it to the if in the for loop.
I have a loop that goes through my gridview and counts the instances where gender field is set to 'M' when a button is clicked.
I would like to also change that color of that row with the same condition, i.e. if the gender is 'M'.
Here is my loop so far and any help would be very appreciated!
protected void Button2_Click(object sender, EventArgs e)
{
int intfemdelegates = 0;
foreach(GridViewRow oItem in GridView1.Rows)
{
if (oItem.Cells[6].Text.Contains('F'))
{
intfemdelegates = intfemdelegates + 1;
GridView1.RowStyle.BackColor = System.Drawing.Color.Red;
}
Label2.Text = Convert.ToString(intfemdelegates);
}
}
You are changing RowColor on whole Grid1. change to this
protected void Button2_Click(object sender, EventArgs e) {
int intfemdelegates = 0;
foreach(GridViewRow oItem in GridView1.Rows) {
if (oItem.Cells[6].Text.Contains('F')) {
intfemdelegates = intfemdelegates + 1;
oItem.BackColor = System.Drawing.Color.Red;
}
Label2.Text = Convert.ToString(intfemdelegates);
}
This code may be useful... good luck!
int intfemdelegates = 0;
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
if (dataGridView1[1, i].Value.ToString() == "F")
{
intfemdelegates = intfemdelegates + 1;
dataGridView1[1, i].Style.BackColor = Color.Red;
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Gray;
}
}
lblcount.Text = intfemdelegates.ToString();
I have a gridview that is bound on page load and I've implemented insert using the footer row technique.
Now I tried the pagination in the same gridview.
I don't have errors but when changing page in the DDL of footer row I find all values duplicates.
In first page the output in DDL is:
MSG
PAY
BUY
LIS
If changed the page the second output in page number two is:
MSG
PAY
BUY
LIS
MSG
PAY
BUY
LIS
I'd greatly appreciate any suggestions.
Thanks!
I used this lines in RowDataBound:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && GridView1.EditIndex == e.Row.RowIndex)
{
DropDownList Area_DDL = (DropDownList)e.Row.FindControl("Area_DDL");
Area_DDL.DataTextField = "area_name";
Area_DDL.DataValueField = "area";
Area_DDL.DataSource = Area();
Area_DDL.DataBind();
Area_DDL.Items.FindByValue((e.Row.FindControl("Area") as Label).Text).Selected = true;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList Area_DDL = (DropDownList)e.Row.FindControl("Area_DDL");
Area_DDL.DataTextField = "area_name";
Area_DDL.DataValueField = "area";
Area_DDL.DataSource = Area();
Area_DDL.DataBind();
}
if (e.Row.RowType == DataControlRowType.Pager)
{
DropDownList ddl = (DropDownList)(e.Row.FindControl("ddlpages"));
Label lblPageCount = (Label)e.Row.FindControl("lblPageCount");
if (lblPageCount != null)
lblPageCount.Text = GridView1.PageCount.ToString();
for (int i = 1; i <= GridView1.PageCount; i++)
{
ddl.Items.Add(i.ToString());
}
ddl.SelectedIndex = GridView1.PageIndex;
if (GridView1.PageIndex == 0)
{
((ImageButton)e.Row.FindControl("ImageButton1")).Visible = false;
((ImageButton)e.Row.FindControl("ImageButton2")).Visible = false;
}
if (GridView1.PageIndex + 1 == GridView1.PageCount)
{
((ImageButton)e.Row.FindControl("ImageButton3")).Visible = false;
((ImageButton)e.Row.FindControl("ImageButton4")).Visible = false;
}
}
}
Change your code like this, I hope it help:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridViewBind();
}
protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow gvrPager = GridView1.BottomPagerRow;
DropDownList ddlPages = (DropDownList)gvrPager.Cells[0].FindControl("ddlPages");
GridView1.PageIndex = ddlPages.SelectedIndex;
GridViewBind();
}
protected void Paginate(object sender, CommandEventArgs e)
{
int intCurIndex = GridView1.PageIndex;
switch (e.CommandArgument.ToString().ToLower())
{
case "First":
GridView1.PageIndex = 0;
break;
case "Prev":
GridView1.PageIndex = intCurIndex - 1;
break;
case "Next":
GridView1.PageIndex = intCurIndex + 1;
break;
case "Last":
GridView1.PageIndex = GridView1.PageCount - 1;
break;
}
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Pager)
{
DropDownList ddl = (DropDownList)(e.Row.FindControl("ddlpages"));
Label lblPageCount = (Label)e.Row.FindControl("lblPageCount");
if (lblPageCount != null)
{
lblPageCount.Text = GridView1.PageCount.ToString();
for (int i = 1; i <= GridView1.PageCount; i++)
{
ddl.Items.Add(i.ToString());
}
ddl.SelectedIndex = GridView1.PageIndex;
if (GridView1.PageIndex == 0)
{
((ImageButton)e.Row.FindControl("ImageButton1")).Visible = false;
((ImageButton)e.Row.FindControl("ImageButton2")).Visible = false;
}
if (GridView1.PageIndex + 1 == GridView1.PageCount)
{
((ImageButton)e.Row.FindControl("ImageButton3")).Visible = false;
((ImageButton)e.Row.FindControl("ImageButton4")).Visible = false;
}
}
}
}
I have a GridView and a list of items in a ListBox. Excluding the first column in the GridView.
I want to tooltip each and every header with the corresponding item in the ListBox. I mean the tooltip for GridView2.Columns[i].HeaderText = ListBox.Items[i].Tostring().
Here is what I have tried:
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 1; i < GridView2.Columns.Count; i++)
{
String header = GridView2.Columns[i].HeaderText;
if (header.Length != 0)
{
e.Row.Cells[i].ToolTip = ListBox4.Items[i].ToString().Trim();
}
}
}
}
This is giving me an exception error: Index was out of range. Must be non-negative and less than the size of the collection.
Kindly help. Thank you.
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 1; i < GridView2.Columns.Count; i++)
{
String header = GridView2.Columns[i].HeaderText;
if (header.Length != 0)
{
e.Row.Cells[i+1].ToolTip = ListBox4.Items[i].ToString().Trim();
}
}
}
}
Just suppose to use Cells[i+1] instead of Cells[i]