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"
Related
I have a gridview and within the item template i have a usercontrol which is losing it's value on postback. I want to be able to access the control from my RowDataBound method in the c# code behind and reassign the CandidateID value
GridView Control
<asp:TemplateField>
<ItemTemplate>
<Controls:LikeButton ID="CandidateLikeButton" runat="server" LikeType="Candidate"
LikedObjectID='<%# Bind("CandidateID") %>' />
</ItemTemplate>
</asp:TemplateField>
How can i do this?
You can access it with FindControl like any other Control and cast it back to it's type.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LikeButton LB = e.Row.FindControl("CandidateLikeButton") as LikeButton;
LB.CandidateID = 1234;
}
}
i am trying to make a "clock in / out site" and one of the functions is for the user to edit old clock ins/outs. i have added a dropdown with 2 values "IND" and "UD". How do i get these values out, and send them to my "CHECKIN" sql value. I hope you know what iam trying to explain.
this is what it looks like:
The Dropdown:
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server"
OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
<asp:ListItem Selected="True">Ind</asp:ListItem>
<asp:ListItem Value="UD">Ud</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
what ive tried:
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
string val = row.Cells[3].ToString();
SqlDataSource1.SelectParameters.Add("CHECKEDIN", val);
}
and aswell as:
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)GridView1.FindControl("DropDownList2");
string val = ddl.SelectedValue;
SqlDataSource1.SelectParameters.Add("CHECKEDIN", val);
}
Both of these return "null" tho
Use RowDataBound event of GridView.
Code
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow
&& GridView1.EditIndex == e.Row.RowIndex)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList2");
string val = ddl.SelectedValue.ToString();
SqlDataSource1.SelectParameters.Add("CHECKEDIN", val);
}
}
You can cast the sender back to a DropDownlist and get the selected value
DropDownList dropDownList = sender as DropDownList;
string val = dropDownList.SelectedValue;
But how will you know with this method which row the DropDownList belongs to?
You can use a trick for that, like abusing the CSS class to get the correct row number.
<asp:DropDownList ID="DropDownList2" runat="server" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" AutoPostBack="true" CssClass='<%# Container.DataItemIndex %>'>
And get the row number in code behind.
int rowNumber = Convert.ToInt32(dropDownList.CssClass);
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.
This is the code in UI
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btn" runat="server" CommandName="Edit" Text="AFE" />
</ItemTemplate>
</asp:TemplateField>
--</Columns>
I Want to Get the Details of All Fields in Text boxes in Same page when ever I click on the Button Edit. I Tried using :
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
GridViewRow Row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
string text = Row.Cells[2].Text;
}
But I am getting "" in string text..
This is screenshot of my grid view
You can use the row index stored in command argument to find a row where Edit button is clicked.
Now you can find the TextBox in that row where cell index location is 2.
Then get the Text of your TextBox.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName == "Edit")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow clickedRow = CustomersGridView.Rows[index];
TextBox myTextBox = (TextBox)clickedRow.Cells[2].FindControl("TextBoxName");
string text = myTextBox.Text;
}
}
In ASPX page, you need to handle row command event like this:
<asp:gridview id="GridView1"
datasourceid="DataSource"
autogeneratecolumns="false"
onrowcommand="GridView1_RowCommand"
runat="server">
</asp:gridview>
You can access to GridViewRow using following code (note: the Button must be the control that has CommandName/CommandArgument):
var row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
Then simply use FindControl method on found row to find your target control, for ex.:
var txtSomething = (TextBox)row.FindControl("txtSomething");
Also you can access to RowIndex using row.RowIndex.
I recommended avoid using row.Cells[INDEX].FindControl(), just row.FindControl works just fine, targeting specific cell isn't good, if you change the markup and add/remove some columns, (maybe) you need to update the INDEX too, so don't do that :)
You need to find the TextBox control in the cell and get the value from it.
string text = (TextBox)Row.FindControl["MyTextBoxId"].Text;
I have read many posts here looking for the answer. But no matter what I do I always get a null value returned for my Label.
I can populate it just fine and read it at that time, but when I try to read the label to populate DB it is always null.
aspx code:
<asp:TemplateField HeaderText="TOTAL YIELD" ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="130">
<EditItemTemplate>
<asp:Label ID="lb_TotalYield" runat="server" ></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
in the code behind I am just trying to read it.
foreach (GridViewRow currentRow in gv_Fruit.Rows)
{
Label tempLabel = (Label)currentRow.FindControl("lb_TotalValue") as Label;
string theTotalValue = tempLabel.Text;
}
for complete information Here is how I set the label:
myGridView.SelectedRow.Cells[4].Text = myTotalYield;
I have tried doing a gv_RowDataBound, but it doesn't seem to ever be called.
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (myGridView.EditIndex == e.Row.RowIndex &&
e.Row.RowType == DataControlRowType.DataRow)
{
Label mylabel = (Label)e.Row.FindControl("lb_TotalYield");
mylabel.DataBind();
}
}