I would like to get name of column which I click and value of this clicked row.
I found very good example but I`m not able to get this data which I want.
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onClick"] = "location.href='view.aspx?id_lekarza=" + DataBinder.Eval(e.Row.DataItem, "Id_lekarza") + " &klikniete=" + "sss" + " '";
}
}
e.Row.RowIndex all the time 0?
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "javascript:alert('" + e.Row.RowIndex + "');");
e.Row.Style.Add("cursor", "pointer");
}
}
In your GridView you can add a template field like below :
<asp:TemplateField ItemStyle-Width="44px" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle"
HeaderStyle-Height="40px" ShowHeader="false">
<ItemTemplate>
<asp:ImageButton ID="btnSelect" runat="server" ImageUrl="~/Images/btnSelect.png"
CommandName="Select" CommandArgument='<%# Eval("ID") %>'/></ItemTemplate>
<HeaderStyle Height="40px"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="44px"></ItemStyle>
</asp:TemplateField>
And in your C# codebehind You should create the RowCommand event of the Gridview , and access the CommandArgument of that row like following:
protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
//You get the ID of that specific row that you have selected :
string ID=e.CommandArgument.ToString();
//Here you write your code ! For example a select query to get that row from database by the ID you have gained in above line of code
}
}
Related
I have a label in an asp GridView and I want to set bind data to it.
Here is my RowDataBound method:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
var name = (Label)e.Row.FindControl("Label_name");
name.Text ="sara";
name.DataBind();
}
and here is my GridView:
<asp:GridView ID="GridViewCertificateType" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" >
<asp:Label ID="Label_name" runat="server" Text="" ></asp:Label>
But after name.Text ="sara";, I receive this exception:
Object reference not set to an instance of an object.
In your RowDataBound method, make sure you have a condition to check what the type of the row is. This event will be called for every row in your GridView, including header and footer rows. If Label_name is not found in the header row, you'll have a null object. Once you do that, get rid of name.DataBind();, as it is not needed.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var name = (Label)e.Row.FindControl("Label_name");
name.Text = "sara";
}
}
try this
var name= (Label)e.Row.Cells[X].FindControl("Label_name");
name.Text ="sara";
name.DataBind();
X means index of column
<asp:Label ID="lblMRNNumber" runat="server" Text='<%# String.Concat(Eval("MRNNumber"))%>'> </asp:Label>
It display as
MRN-01
MRN-02
MRN-03
my requirement is
'MRN-01'
'MRN-02'
'MRN-03'
Text='<%# String.Concat("'",Eval("MRNNumber"),"'")%>' This gives error!
how to do this!
Try this
Text="<%#(Eval("MRNNumber","'{0}'"))%>"
This will be treated as
text=Eval("MRNNumber","'{0}'")
According to Gridview Item formatting:
Method 1:
Use BoundField, and intercept the GridView's RowDataBound event, in the
RowDataBound event, we can get the Binded Data from the certain GridView
Row's Cell
For Ex:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[2].Text = "'" + e.Row.Cells[2].Text + "'";
}
}
or:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl_Name = (Label)e.Row.FindControl("lblMRNNumber");
lbl_Name.Text = "'" + lbl_Name.Text + "'";
}
}
Method 2:
<asp:TemplateField HeaderText="TemplatePrice">
<ItemTemplate>
<asp:Label ID="lblMRNNumber" runat="server" Text='<%# AddDollar(Eval("MRNNumber").ToString()) %>'> </asp:Label>
</ItemTemplate>
</asp:TemplateField>
AddDolloar is the helper function defined in page class:
protected string AddDollar(string mystr)
{
return "'" + mystr + "'";
}
Take a look at this link
I need to delete a row from datatable according to the selected value from the grid view
I tried to make it, but I failed
protected void GV_Repair_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
var row = GV_Repair.Rows[e.RowIndex];
newdt.Rows.Remove();
bind_grid();
}
If you have a static data source, you can do this:
protected void GV_Repair_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
newdt.Rows.RemoveAt(e.RowIndex);
bind_grid();
}
If the datasource is dynamic, you will need a hidden field (if you don't have one already) in GridView's template field:
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hdnId" runat="server" Value='<%# Eval("ID") %>' />
<!-- Other Items -->
</ItemTemplate>
</asp:TemplateField>
And change the row deleting method:
protected void GV_Repair_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = 0;
GridViewRow row = GV_Repair.Rows[e.RowIndex];
HiddenField hdnId = row.FindControls("hdnId") as HiddenField;
if(hdnId != null && int.TryParse(hdnId.Value, out id)
{
//Logic to delete from table where Id = id
}
bind_grid();
}
I have a checkbox in my gridview footer with autopostack=true (which should fire a checkedchanged event), and a linkbutton in my itemtemplate (which should fire the gridview rowcommand event).
Everything has been working fine, until I placed the following code in my gridview rowdatabound (or databound) event:
for (int i = 0; i < gridCartRows.Columns.Count - 2; i++)
{
e.Row.Cells.RemoveAt(0);
}
e.Row.Cells[0].ColumnSpan = gridCartRows.Columns.Count - 1;
Now, when I click my linkbutton, the checkbox checkedchanged event is automatically fired, and then the rowcommand event is fired.
Why is the checkedchanged event being fired when it shouldn't, when I add the above code?
Is there a way to get around this?
Edit
Here's a sample, that reproduces my issue:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowFooter="True"
OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Column1">
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column2">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" Text="Fire Row Command" CommandName="Fire" />
</ItemTemplate>
<FooterTemplate>
Footer
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column3">
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code-behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = new int[5];
GridView1.DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
for (int i = 0; i < GridView1.Columns.Count - 2; i++)
{
e.Row.Cells.RemoveAt(0);
}
e.Row.Cells[0].ColumnSpan = GridView1.Columns.Count - 1;
((CheckBox)e.Row.FindControl("CheckBox1")).Checked = true;
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Fire")
{
Response.Write("RowCommand fired.");
}
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
Response.Write("CheckBox fired.");
}
Note, I'm setting the CheckBox property to true in the RowDataBound - if I remove this, it works fine. So merging the cells and setting the checkbox property aren't working nicely together.
GridView Control events are stored in viewstate and they can get messed up very easily. Here removing a cell causing to bind different event to link button. Although after postback __EVENTTARGET is LinkButton, wrong method is called and wrong control, event arguments are passed.
I would suggest to hide the cell rather than removing it:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].ColumnSpan = GridView1.Columns.Count - 1;
e.Row.Cells[0].Visible = false;
((CheckBox)e.Row.FindControl("CheckBox1")).Checked = true;
}
}
This will serve the purpose.
How can I implement the following: I would like to have a checkbox on the top of my gridview. When checkbox is checked should show all the hidden rows from the gridview but when is unchecked should only show the unhide ones.
I only need 1 checkbox at the top(not in the header of the gridview), I found couple of examples but all of the have checkbox as column and then added to the header as well.
What would be the best way to do this?
Thanks in advance.
This is a one way of doing it on the server:
I define the Gridview and Checkbox as follows:
<asp:CheckBox id="cbShowHidden" runat="server" Text="Show Hidden Rows"
Checked="true" oncheckedchanged="cbShowHidden_CheckedChanged" AutoPostBack="true" ></asp:CheckBox>
<br />
<asp:GridView ID="gvTest" AutoGenerateColumns="false" runat="server"
ShowHeader="true" onrowdatabound="gvTest_RowDataBound">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" />
<asp:BoundField DataField="name" HeaderText="name" />
<asp:BoundField DataField="family" HeaderText="family" />
<asp:BoundField DataField="visibility" HeaderText="visibility" />
</Columns>
</asp:GridView>
This is the codebehind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
PopulateGrid();
}
}
private void PopulateGrid()
{
gvTest.DataSource = Enumerable.Range(0, 3000).Select(i => new { id = i, name = 2 * i, family = "Unknown", visibility = i % 3 == 0 ? "Visible" : "Hidden" });
gvTest.DataBind();
}
protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs e)
{
bool showHidden = cbShowHidden.Checked;
if (e.Row.RowType == DataControlRowType.DataRow)
e.Row.Visible = showHidden || string.Compare(e.Row.Cells[3].Text, "Visible") == 0;
}
protected void cbShowHidden_CheckedChanged(object sender, EventArgs e)
{
PopulateGrid();
}
This can be greatly optimized by doing the hiding on the client with jQuery.