I have a gridview which displays a data from the database. In one of the tables in this database have a column to store details about attanchment file. If the attachment is available that column value will set as "YES". Otherwise it will set as "NO". What I want to do is, show a link to view the attachment. But if cell value of the column is "NO" (when there is no attachment) the link must be hidden.
Note : I know how to view a file. Here what Im expecting is to hide the link in the cell which doesn't have an attachment.
This is what I have done upto now.
if (ds.Tables[0].Rows.Count > 0)
{
grdSo.DataSource = ds;
grdSo.DataBind();
for(int i=0; i <ds.Tables[0].Rows.Count; i++)
{
if (ds.Tables[0].Rows[i][6].Equals("NO"))
{
grdSo.Rows[i].Cells[6].Visible = false;
}
else
{
grdSo.Rows[i].Cells[6].Visible = true;
}
}
}
I could hide the cell using this code. But unfortunately this hides the lines of the cell too. How can I avoid it happening?
One of the ways to do this is to use server side control like LinkButton to view the link that you want to show and set the visibility of the control as per your requirement in the OnRowDataBound event of the gridview.
Below is the code to show/hide LinkButton on OnRowDataBound event.
protected void gridId_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataSourceClass varData = (DataSourceClass)e.Row.DataItem;
// check if your data have flag to show the link
if(varData.show)
((LinkButton)e.Row.FindControl("linkbuttonId")).Visible = true;
else
((LinkButton)e.Row.FindControl("linkbuttonId")).Visible = false;
}
}
ASPX Code :
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField Visible="false" DataField="id" />
<asp:TemplateField HeaderText="Has Attachment">
<ItemTemplate>
<asp:Label ID="lblAtt" runat="server" Text='<%#Eval("HasAtt") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="View Attachment">
<ItemTemplate>
<asp:LinkButton ID="lbtnAtt" runat="server" OnClick="lbtnAtt_Click" Visible="false">View</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
CS Code :
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
//Here you write databind logic
// Datasource table of GridView1 should contain 'HasAtt' and 'id' as its binded to label.
}
private void ViewAttachment(int id)
{
//Here you write your logic to view attachment.
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow) // checking if row is datarow or not
{
Label lblHasAtt = e.Row.FindControl("lblAtt") as Label;
LinkButton lbtnViewAtt = e.Row.FindControl("lbtnAtt") as LinkButton;
lbtnViewAtt.Visible = (lblHasAtt.Text.ToLower() == "yes");
}
}
protected void lbtnAtt_Click(object sender, EventArgs e)
{
LinkButton lbtnViewAtt = sender as LinkButton;
GridViewRow grw = lbtnViewAtt.NamingContainer as GridViewRow;
int id = Convert.ToInt32(this.GridView1.Rows[grw.RowIndex].Cells[0].Text); // retriving value of first column on 'lbtnAtt' click row.
this.ViewAttachment(id);
}
I recall being able to parse through the grid view by using a for each for the rows and using a for statement for columns.
If I recall correctly, you can grab an item ie
row.Item[i]
foreach(GridViewRow row in GridView1.Rows)
{
for(int i = 0; i < GridView1.Columns.Count; i++)
{
// here you can do the logic to decide whether to show or hide the text for this cell
}
}
Sorry for formatting responding on my phone.
Related
I have a DataGridView with a column which with button.
Now I want that with every click on the button the selected row in the DataGridView is copied and inserted directly after the selected row.
I want to duplicate all value of all columns and insert into table of my database this new row.
How can I realize this ?
Many thanks in advance.
On my code below the error is on this line
cgv.Rows.Add(gvRow.Cells[1].Text);
<asp:GridView runat="server" ID="gvCustomers" AutoGenerateColumns="false"
OnSelectedIndexChanged="OnSelectedIndexChanged">
<Columns>
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:ImageButton ID="btselect" runat="server"
CommandName="Select"
OnClick="Copy"
ImageUrl="/aspnet/img/clone_icon.gif"
ToolTip="Clone row" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CustomerId" HeaderText="ID" />
</Columns>
</asp:GridView>
protected void Copy(object sender, EventArgs e)
{
GridViewRow gvRow = this.gvProducts.SelectedRow;
DataTable cgv = null;
if (ViewState["Customers"] != null)
{
cgv = ViewState["Customers"] as DataTable;
cgv.Rows.Add(gvRow.Cells[1].Text);
ViewState["Customers"] = cgv;
}
BindData();
}
protected void gvProducts_SelectedIndexChanged(object sender, EventArgs e)
{
this.gvProducts.SelectedRow.BackColor = System.Drawing.Color.Cyan;
}
Ok, you are in a good place in that you attempting to add the data row to the data source, or the datatable that you are persiting in session.
So, I would do it this way:
protected void Copy(object sender, EventArgs e)
{
GridViewRow gvRow = this.gvProducts.SelectedRow;
DataTable cgv = null;
if (ViewState["Customers"] != null)
{
cgv = ViewState["Customers"] as DataTable;
DataRow MyNewRow = cgv.NewRow()
MyNewRow["FirstName"] = gvRow.Cells[2].Text;
MyNewRow["LastName"] = gvRow.Cells[3].Text;
MyNewRow["CustomerID"] = gvRow.Cells[0];
cgv.Rows.Add(MyNewRow);
ViewState["Customers"] = cgv;
}
BindData();
}
Do remember that any template field in the GV needs to use find control, but from what you posted, it does look like using .cells[] collection should work for you.
i have gridview with 3 columns and 100 rows. and one submit button out of gridview.
here first 2 column is bounded field and 3rd one is Label (templatefield) .
Now, I want to change status of 3rd column and display it to gridview when loop run.
i want to
when i=0 then i want to change label's value to "SUCESS" and display on Gridview,
i=1 then i want to change label's value to "SUCESS" and display on Gridview,
and so on till i=100.
Gridview
<asp:GridView ID="GvCategoryLive" runat="server" AutoGenerateColumns="False" EmptyDataText="No Records Found !" >
<Columns>
<asp:BoundField DataField="UnitName" HeaderText="Unit Name" />
<asp:BoundField DataField="CreateDate" HeaderText="Created Date" />
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="UnitMesurement_Id" runat="server" Text=""></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I am trying something like this
protected void btnSave_Click(object sender, EventArgs e)
{
if (btnSave.Text == "Save")
{
for (int i = 0; i < GvCategoryLive.Rows.Count; i++)
{
System.Threading.Thread.Sleep(9000);
GvCategoryLive.Rows[i].Cells[2].Text = "Success";
}
}
}
I really dont remember the gridviews and asp.net webforms but
You may use setTimeout in javascript
var rowCount = 100;// get row count here
for(int i = 0; i < rowCount ; i++)
{
setTimeout($('#cell'+i).html('Success'), 9000);
}
You could try like this:
protected void btnSave_Click(object sender, EventArgs e)
{
if (btnSave.Text == "Save")
{
foreach(GridViewRow row in GvCategoryLive.Rows) {
if(row.RowType == DataControlRowType.DataRow) {
Label UnitMesurement_Id= row.FindControl("UnitMesurement_Id") as Label ;
UnitMesurement_Id.Text = "Success";
}
}
}
}
Because you are not setting the value of a cell but label inside that cell, you should change this-
GvCategoryLive.Rows[i].Cells[2].Text = "Success";
into-
Label UnitMesurement_Id = GvCategoryLive.Rows[i].FindControl("UnitMesurement_Id") as Label;
UnitMesurement_Id.Text = "Success";
I have a GridView. Every row has a textbox and a radiobutton (3 options)
If the radiobutton is selected then the textbox.text = ""
Problem: when OnSelectedIndexChanged is called every textbox inside my grid goes blank
How can I clear only the textbox of the row I selected the radiobutton in?
ASPX markup
<asp:GridView id="mygrid" Runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButtonList ID="hi" runat="server"
OnSelectedIndexChanged="zzz" AutoPostBack="true" />
<asp:TextBox ID="txPregoeiro" runat="server" Text="." />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
C# code-behind
protected void zzz(object sender, EventArgs e)
{
foreach (GridViewRow _row in mygrid.Rows)
{
if (_row.RowType == DataControlRowType.DataRow)
{
RadioButtonList hi = (RadioButtonList)_row.FindControl("hi");
TextBox txPregoeiro = (TextBox)_row.FindControl("txPregoeiro");
txPregoeiro.Text = string.Empty;
}
}
}
Your not checking to see if the radio button list has a selected item or not. As a result you are always setting the textbox text to blank. Change the function to:
GridViewRow _row = mygrid.SelectedRow;
if (_row.RowType == DataControlRowType.DataRow)
{
RadioButtonList hi = (RadioButtonList)_row.FindControl("hi");
if(hi.SelectedItem != null) //This checks to see if a radio button in the list was selected
{
TextBox txPregoeiro = (TextBox)_row.FindControl("txPregoeiro");
txPregoeiro.Text = string.Empty;
}
}
You are currently doing it for every row which will clear every text box. Give this a try.
protected void zzz(object sender, EventArgs e)
{
var caller = (RadionButtonList)sender;
foreach (GridViewRow _row in mygrid.Rows)
{
if (_row.RowType == DataControlRowType.DataRow)
{
RadioButtonList hi = (RadioButtonList)_row.FindControl("hi");
if(hi == caller)
{
TextBox txPregoeiro = (TextBox)_row.FindControl("txPregoeiro");
txPregoeiro.Text = string.Empty;
break; //a match was found break from the loop
}
}
}
}
I have a gridview which is being loaded from database table.
There is one column in the table named 'valid',whose values are 'Y' or 'N'.
Now, what I want to do is, to check if the value of 'valid' is Y, then the whole record in that row should appear as disabled when loading the gridview.
How to proceed with that? should I write a procedure for this or it can be done from code behind?
Note: the column Valid is not a part of gridview.
You need to use RowDataBound event of gridview to disable rows.
Note that if you use databound columns then it is rendered in tr and td then you need to find the cell and then set the disable property.
protected void gvEntity_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//CheckBox cbAttachClrReq = (CheckBox) e.Row.FindControl("chkAdd");
//check the value here and set enable property
e.Row.Enabled = false;
}
}
As #Hanlet EscaƱo has suggested you can add the field value in the same column as follows
<asp:TemplateField>
<ItemTemplate>
<<asp:Label ID="lbl1" runat="server"
Value='<%# Eval("Name") %>' />
<asp:HiddenField ID="HiddenField1" runat="server"
Value='<%# Eval("valid") %>' />
</ItemTemplate>
and get the hidden field value in row-databound event as follows
protected void gvEntity_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HiddenField Hf = (HiddenField) e.Row.FindControl("HiddenField1");
if(Hf.Value=="Y")
e.Row.Enabled = false;
}
}
Try the following.
This example gets the DataRowView per record and then extracts the valid column from the underlying datasource to test against.
protected void grdDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var dr = e.Row.DataItem as DataRowView;
if(Convert.ToString(dr["valid"]) == 'Y')
e.Row.Enabled = false;
}
}
and the markup
<asp:GridView ID="grdDetails" runat="server" OnRowDataBound="grdDetails_RowDataBound"
i have a gridview which is inside an update panel. when i click "cance" after the edit, nothing happens. When i debug it does go into my gvWorkhours_RowCommand function and inside the cancel if, but nothing happens on screen (the edit fields are all still visible)
here is what i have:
<asp:GridView ID="gvWorkhours" runat="server" AutoGenerateColumns="false" CssClass="GridViewStyle" OnRowEditing="gvWorkhours_RowEditing"
OnRowCommand="gvWorkhours_RowCommand" >
<EmptyDataTemplate>
no data returned
</EmptyDataTemplate>
<Columns>
<asp:commandfield buttontype="Link" showeditbutton="true" edittext="Edit" />
<asp:TemplateField HeaderText="Organization">
<ItemTemplate>
<%# Eval("org.orgCode").ToString() + "- " + Eval("org.orgSubCode").ToString()%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Year-Qtr">
<ItemTemplate>
<%# Eval("year").ToString() + "- " + Eval("qtr").ToString()%>
</ItemTemplate>
</asp:TemplateField>
.......
protected void gvWorkhours_RowEditing(Object sender, GridViewEditEventArgs e)
{
populateGrid(); //pulls from the Db and binds to the grid
gvWorkhours.EditIndex = e.NewEditIndex; //This is the selected row to edit
gvWorkhours.DataBind(); //Make the edit Template show up
}
protected void gvWorkhours_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if (e.CommandName == "Cancel")
{
gvWorkhours.EditIndex = -1;
populateGrid();
}
}
private void populateGrid()
{
//getting all variables for update here.
Workhours wh = new Workhours(selectedItem, year, qtr);
gvWorkhours.DataSource = wh.exposures;
gvWorkhours.DataBind();
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
// throw(ex);
}
}
what am i missing here?
You don't have the UpdatePanel shown in your example, so I don't know how you have it set up, but if you have the UpdateMode set to Conditional, you probably need to manually call the Update method on the update panel:
if (e.CommandName == "Cancel")
{
gvWorkhours.EditIndex = -1;
populateGrid();
UpdatePanel1.Update(); // whatever the name of the UpdatePanel is
}
try to give other word than cancel like "CancelRecord" and then try with it
Yes it's an old question, but no answer.
Cancel needs to be in a RowCancelEdit method ... seems 'Cancel' is a protected word in this scenario
protected void gvResults_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView gv = (GridView)sender;
gv.EditIndex = -1;
BindGrid2();
}