get header for the cell - c#

i have this
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Style.Add("text-align", "center");
}
i want to make the header of this cell invisible.. how can i do this by just knowing the cell value...
it does not find the header... i am creating the header at runtime... like this
GridView Grid = new GridView();
Grid.RowDataBound += Grid_RowDataBound;
Grid.ID = machGrps[j].ToString();
//Grid.AutoGenerateColumns = false;
Grid.AllowSorting = false;
Grid.CellSpacing = 2;
Grid.ForeColor = System.Drawing.Color.White;
Grid.GridLines = GridLines.None;
Grid.Width = Unit.Percentage(100);
Grid.Style.Add(HtmlTextWriterStyle.Overflow, "Scroll");
Grid.ShowHeader = true;
DataTable taskTable = new DataTable("TaskList7");
taskTable.Columns.Add("MachineID");
DataRow tableRow = taskTable.NewRow();
tableRow["MachineID"] = machID[i];
taskTable.Rows.Add(tableRow);
Grid.DataSource = taskTable;
Grid.DataBind();
protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Visible = false;
}
}

Sorry for VB.Net :
Select Case e.Row.RowType
Case DataControlRowType.Header
e.Row.Cells(0).Visible = False
End Select
Edit: C#
if (e.Row.RowType == DataControlRowType.Header){
e.Row.Cells(0).Visible = False;
}

Related

How to check bool value in Rowdatabound event of gridview in asp.net

I have a gridview and I am binding the data through Datareader. In database t able this is a bit field and I want to check this that if it's FALSE hide some controls in gridview. so how can I get this value while Rowdatabound event. Thank you
here is my code.
protected void AllUsersGridView_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton LinkButton1 = (LinkButton)e.Row.Cells[6].FindControl("LinkButton1");
LinkButton LinkButton2 = (LinkButton)e.Row.Cells[6].FindControl("LinkButton2");
CheckBox c = ((CheckBox)e.Row.Cells[2]);
if (c.Checked)
{
LinkButton1.Visible = true;
LinkButton2.Visible = true;
}
else
{
LinkButton1.Visible = false;
LinkButton2.Visible = false;
}
}
}
You can directly use database field as mentioned below:
protected void AllUsersGridView_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton LinkButton1 = (LinkButton)e.Row.Cells[6].FindControl("LinkButton1");
LinkButton LinkButton2 = (LinkButton)e.Row.Cells[6].FindControl("LinkButton2");
DataRow row = ((DataRowView)e.Row.DataItem).Row;
bool isChecked = row.Field<bool>("[FiledName]");
// CheckBox c = ((CheckBox)e.Row.Cells[2]);
LinkButton1.Visible = isChecked;
LinkButton2.Visible = isChecked;
}
}
See below two lines of code in above code:
DataRow row = ((DataRowView)e.Row.DataItem).Row;
bool isChecked = row.Field<bool>("[FiledName]");
Try this
protected void AllUsersGridView_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton LinkButton1 = (LinkButton)e.Row.FindControl("LinkButton1");
LinkButton LinkButton2 = (LinkButton)e.Row.FindControl("LinkButton2");
CheckBox c = ((CheckBox)e.Row.FindControl("idOfCheckBox");
LinkButton1.Visible = LinkButton2.Visible = c.Checked
}
}

AutocompleteSource in datagridviewtextBoxColumn

I'm trying to add AutoCompleteSource in datagridViewtextBoxColumn. I'm trying two methods.In the first one i'm directly adding AutoCompleteSource to datagridViewColumn. And in the second i created a textBox on the desired cell and added AutocompleteCustome Source. But none of this working with no exception.
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
DataGridViewCell cel = dataGridView1.CurrentCell;
DataGridViewRow row = dataGridView1.CurrentRow;
if (e.Control.GetType() == typeof(DataGridViewTextBoxEditingControl))
{
if (cel == row.Cells[1])
{
DataGridViewTextBoxEditingControl t = e.Control as DataGridViewTextBoxEditingControl;
AutoCompleteStringCollection ccl = new AutoCompleteStringCollection();
foreach (DataRow rw in bowoniDataSet17.item.Rows)
{
ccl.Add(rw.ToString());
}
t.AutoCompleteSource = AutoCompleteSource.CustomSource;
t.AutoCompleteCustomSource = ccl;
t.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
}
}
}
TextBox tb = new TextBox();
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
DataGridViewRow row = dataGridView1.CurrentRow;
DataGridViewCell cel = dataGridView1.CurrentCell;
Rectangle rect=dataGridView1.GetCellDisplayRectangle(dataGridView1.CurrentCell.ColumnIndex,dataGridView1.CurrentCell.RowIndex,true);
tb.Size = new Size(rect.Width, rect.Height);
tb.Location = new Point(rect.X, rect.Y);
tb.TextAlignChanged += new EventHandler(tbtx_OnTextChanged);
if (cel == row.Cells[1])
{
AutoCompleteStringCollection ccl = new AutoCompleteStringCollection();
foreach (DataRow rw in bowoniDataSet17.item.Rows)
{
ccl.Add(rw.ToString());
}
tb.AutoCompleteSource = AutoCompleteSource.CustomSource;
tb.AutoCompleteCustomSource = ccl;
tb.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
}
}
private void tbtx_OnTextChanged(object sender, EventArgs e)
{
dataGridView1.CurrentCell.Value = tb.Text;
}
Now i changed it to DataGridViewComboxColumn.Working fine.

Create a Header and Sub-Header in Gridview

This is my desired output
However based on my code this is the current output
Here are my codes
protected void grdVESONV_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
GridView HeaderGrid = (GridView)sender;
GridViewRow HeaderGridRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
TableCell HeaderCell = new TableCell();
HeaderCell.Text = "Header 4";
HeaderCell.ColumnSpan = 2;
HeaderGridRow.Cells.Add(HeaderCell);
grdVESONV.Controls[0].Controls.AddAt(0, HeaderGridRow);
}
}
This can be implemented by writing an event, OnDataBound as protected void OnDataBound(object sender, EventArgs e) and use the RowSpan wherever is needed.

How to hide row in GridView if certain column is empty?

I have a GridView that displays some information about a job and one of the options in the GridView is to select a reason. If a reason isn't selected then I don't want to display that row in the GridView. How do I check if there is a value in the Reason column, and if its empty how do I hide that row?
private GridView BuildDebriefGridView()
{
GridView gv = new GridView();
gv.AutoGenerateColumns = false;
//gv.RowDataBound +=
gv.Columns.Add(new BoundField { HeaderText = "Job No", DataField = "JobNo" });
gv.Columns.Add(new BoundField { HeaderText = "Qty Rcvd", DataField = "QtyRcvd" });
gv.Columns.Add(new BoundField { HeaderText = "Reason", DataField = "Reason" });
gv.Columns.Add(new BoundField { HeaderText = "Comment", DataField = "Comment" });
gv.Attributes.Add("style", "width:100%");
return gv;
}
void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
JobPieceSerialNo item = e.Row.DataItem as JobPieceSerialNo;
if (item != null)
{
if (string.IsNullOrEmpty(item.Reason))
{
e.Row.Visible = false;
}
}
}
}
Try following;
private void gvTransportListResults_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.Cells["Reason"].Text == "")
e.Row.Visible = false;
}
Or if it is a checkbox
private void gvTransportListResults_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(((CheckBox)e.Row.FindControl("YourCheckboxID")).Checked == false)
e.Row.Visible = false;
}
Using GridView_RowDataBound event you can achieve this.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[ReasonCellNumber].Text == "Reason")
{
e.Row.Visible = false;
}
}
}

Dynamically added button is not firing event and is losing state dispite re-adding it in pre_init event

I have a dropdown which is bound to a database. On its index change there is a function that add some button in a panel based upon selected value.
I am reading those button in page_init event but still I get null values, i.e. event bound with the button never fires.
Here is my code and dropdownlist1 is the dropdown that is adding dynamic button.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
colorgroupsTableAdapters.master_color_groupTableAdapter ta
= new colorgroupsTableAdapters.master_color_groupTableAdapter();
DataTable dt = ta.GetData();
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = dt.Columns[1].ToString();
DropDownList1.DataValueField = dt.Columns[0].ToString();
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("Select One", "0"));
}
}
protected void Page_Init(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
bindcolors();
}
}
protected void DropDownList1_DataBound(object sender, EventArgs e)
{
}
protected void DropDownList2_DataBound(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex < 1)
{
DropDownList2.Items.Clear();
}
DropDownList2.Items.Insert(0, new ListItem("Select One", "0"));
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
ViewState["dd"] = DropDownList1.SelectedIndex;
bindcolors();
}
void bindcolors()
{
if (DropDownList1.SelectedIndex > 0)
{
addcolorgroupsTableAdapters.groupavailablecolorTableAdapter ta
= new addcolorgroupsTableAdapters.groupavailablecolorTableAdapter();
DataTable dt = ta.GetData(int.Parse(DropDownList1.SelectedValue));
HtmlTable ht = new HtmlTable();
ht.Width = "90%";
ht.Border = 1;
for (int i = 0; i < dt.Rows.Count; i++)
{
HtmlTableRow tr = new HtmlTableRow();
HtmlTableCell tc1 = new HtmlTableCell();
HtmlTableCell tc2 = new HtmlTableCell();
HtmlTableCell tc3 = new HtmlTableCell();
object[] ob = dt.Rows[i].ItemArray;
tc1.InnerHtml = ob[0].ToString();
tc2.InnerHtml = ob[1].ToString();
tc2.BgColor = "#" + ob[1].ToString();
Button b = new Button();
b.Text = "Remove";
b.CommandArgument = ob[0].ToString();
AjaxControlToolkit.ConfirmButtonExtender cb
= new AjaxControlToolkit.ConfirmButtonExtender();
cb.ConfirmText = "Are You Sure To Delete This Color From The Group?";
b.ID = "Bo" + ob[0].ToString();
b.EnableViewState = true;
b.Click += new EventHandler(b_Click);
cb.TargetControlID = "Bo" + ob[0].ToString();
tc3.Controls.Add(b);
tc3.Controls.Add(cb);
tr.Cells.Add(tc1);
tr.Cells.Add(tc2);
tr.Cells.Add(tc3);
ht.Rows.Add(tr);
}
Panel1.Controls.Add(ht);
}
}
void b_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
int grp = int.Parse(DropDownList1.SelectedValue);
int clid = int.Parse(b.CommandArgument);
addcolorgroupsTableAdapters.QueriesTableAdapter ta
= new addcolorgroupsTableAdapters.QueriesTableAdapter();
ta.DeleteQuery_group_color(grp, clid);
DropDownList2.DataBind();
bindcolors();
}
protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex > 0 && DropDownList2.SelectedIndex > 0)
{
int grp = int.Parse(DropDownList1.SelectedValue);
int clid = int.Parse(DropDownList2.SelectedValue);
addcolorgroupsTableAdapters.QueriesTableAdapter ta
= new addcolorgroupsTableAdapters.QueriesTableAdapter();
ta.Insert_into_group_color(grp, clid);
DropDownList2.DataBind();
bindcolors();
}
}
Please tell what I am doing wrong?
I think the problem is the check for SelectedIndex > 0 in bindControls. The reason is that ViewState is evaluated between the Init and Load so the value for the SelectedIndex property is not set yet (and therefore = -1).
You could try a different approach: use a Repeater control that is databound to the results of the database query. This way, the general structure can be defined in the Repeater and its ItemTemplate. Also the EventHandlers are registered in the ItemTemplate.
You can reset the DataSource of the Repeater whenever the SelectedIndex of the Combobox changes and do not need to recreate the controls dynamically in init.
Your code should be much shorter and behave more deterministic.

Categories

Resources