I need to export to XLS the selected row of my DevExpress GridView.
When i try to export my grid, it creates an empty file. I think it is due to the callback that clear the data in my grid before exporting.
This is samples of my ASPx page.
The Load data button
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Styles/Images/load.png" onclick="ImageButton1_Click" />
The gridview
<dx:ASPxGridView ID="grid" ClientInstanceName="grid" runat="server" AutoGenerateColumns="False" KeyFieldName="car">
<Columns>
<dx:GridViewCommandColumn ShowSelectCheckbox="True" VisibleIndex="0" Caption="">
</dx:GridViewCommandColumn>
<dx:GridViewDataTextColumn Caption="Category" VisibleIndex="1" FieldName="category"
Name="category" GroupIndex="0" SortIndex="0" SortOrder="Ascending">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn Caption="Car" VisibleIndex="2" FieldName="car" Name="car">
</dx:GridViewDataTextColumn>
</Columns>
<SettingsPager PageSize="100">
</SettingsPager>
<Settings ShowFooter="True" />
<GroupSummary>
<dx:ASPxSummaryItem FieldName="car" SummaryType="Count" />
</GroupSummary>
</dx:ASPxGridView>
The GridExporter
<dx:ASPxGridViewExporter ID="gridExport" runat="server" GridViewID="grid" ExportedRowType="Selected" />
The create file button
<dx:ASPxButton ID="createFile" runat="server" Text="Create File" UseSubmitBehavior="False" OnClick="createFile_Click">
<Image Url="~/Styles/Images/save.png">
</Image>
</dx:ASPxButton>
Now the code behind.
When I click the load button, i create fake datas for my tests.
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
CreateFakeData();
}
I create my datas in a DataTable Object. And then i bind my dataTable with my grid.
grid.DataSource = fakes;
grid.DataBind();
Everything seem to works great but when i click the export button, nothing is exported.
protected void createFile_Click(object sender, EventArgs e)
{
gridExport.WriteXlsToResponse();
}
I followed the DevExpress tutoria to export selected rows in ASPl. But it seems that my page is refreshed so i loose the data binded with the grid before the export.
I had a similar situation when I was binding data to an ASPXGridView in the code behind. I was able to solve it by rebinding in the button click, before the export line.
protected void createFile_Click(object sender, EventArgs e)
{
grid.DataSource = fakes;
grid.DataBind();
gridExport.WriteXlsToResponse();
}
The problem was with the postback, as I expected, I needed a way to keep my gridView Datas somewhere and reload it after the postback.
I added the OnDataBinding attribute to my gridView
<dx:ASPxGridView ID="grid" ClientInstanceName="grid" runat="server"
AutoGenerateColumns="False" Width="100%" Caption="My car collection"
OnDataBinding="grid_DataBinding" KeyFieldName="car_model">
In the code behind, I coded the grid-DataBinding function so it restores the data using the data stocked in the session previously.
protected void grid_DataBinding(object sender, EventArgs e)
{
if (Session["gridData"] != null)
{
// Assign the data source in grid_DataBinding
grid.DataSource = Session["gridData"];
}
}
Don't forget to save the data in the session when desired. (I did it right after the search)
protected void searchBtn_Click(object sender, ImageClickEventArgs e)
{
datas = FindCars(input.Text);
grid.DataSource = datas;
grid.DataBind();
//Store data in the session
Session["gridData"] = this.datas;
}
Related
while updating gridview record . old values only getting updated. im using bound field. getting old value while im fetching data in variable.
<asp:GridView runat="server" ID="GvLeads" AutoGenerateColumns="false" AutoGenerateEditButton="true" AutoGenerateDeleteButton="true" OnRowDeleting="GvLeads_RowDeleting" OnRowEditing="GvLeads_RowEditing" OnRowCancelingEdit="GvLeads_RowCancelingEdit" EmptyDataText="No Records Found" OnRowUpdating="GvLeads_RowUpdating" OnRowDeleted="GvLeads_RowDeleted">
<Columns>
<asp:BoundField HeaderText="Id" DataField="LeadId" />
<asp:BoundField HeaderText="Company" DataField="Companyname" />
</Columns>
</GridView >
code behind
protected void GvLeads_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GvLeads.Rows[e.RowIndex];
String str = ((TextBox)(row.Cells[1].Controls[0])).Text;
int Leadid = Convert.ToInt32(str);
string CompanyName = ((TextBox)(row.Cells[2].Controls[0])).Text;
}
This usually happens when you are populating grid at Page_Load as soon as RowUpdating event gets called before that Page_Load event get's called which populates the grid with initial values. How to Avoid? Use !IsPostBack for this purpose
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid(); // For e.g.
}
}
this is my first time working with the Code First approach with Entity Framework Models. I'm trying to populate a data grid with user info but the grid column isn't displaying any info.
<dx:ASPxGridView ID="Customer_Grid" runat="server" AutoGenerateColumns="False" Width="170px" >
<Columns>
<dx:GridViewDataTextColumn Name="CustomerName" VisibleIndex="0">
</dx:GridViewDataTextColumn>
</Columns>
</dx:ASPxGridView>
Here's my code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ecdevelopmentEntities DB = new ecdevelopmentEntities();
Customer_Grid.DataSource = DB.Customers.ToList();
Customer_Grid.DataBind();
}
}
I am displaying data from my table (one column) in a grid, but the data is large (about 50 entries). Can I display the data in multiple columns/ rows so that it is easy for the user to read without scrolling down?
Here's my code.
ASPX:
<asp:GridView AlternatingRowStyle-BackColor="Wheat"
ID="GDV_1" runat="server"
EnableModelValidation="true"
AutoGenerateColumns="false"
OnSelectedIndexChanged="GDV_1_SelectedIndexChanged"
AutoGenerateSelectButton="false" DataKeyNames="SROID">
<Columns>
<asp:HyperLinkField DataTextField="SRONameinEnglish"
HeaderText="SRO Name"
DataNavigateUrlFields="SROID"
DataNavigateUrlFormatString="Webredirect2.aspx?SROID={0}" />
</Columns>
</asp:GridView>
ASPX.CS
protected void btn_submit_Click(object sender, EventArgs e)
{
FeesBLL p = new FeesBLL();
GDV_1.DataSource = p.table(txt_1.Text);
//GDV_1.Columns[1].Visible = true;
GDV_1.DataBind();
}
Kindly give you suggestion fr achieving the required result.
I am making a web site in visual studio 2012, using asp.net, c#.
I created a grid view, which is taking data from my sql server, and created a button field which is bound to id_p, which is one of the collumns taken from a database. I am trying to get the data of the id_p for the row in which the button was clicked.
<asp:ButtonField ButtonType="Button" DataTextField="id_p"
DataTextFormatString="Stavi u košaricu" Text="Button1" />
What I need is not the selected row, only the id_p value, so how could I do that, please?
You need to handle the OnRowCommand Event on the gridview as so:
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView_RowCommand"
And create a ItemTemplateField to display a regular <asp:button> instead of using the ButtonField column:
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btn" runat="server"
CommandName="Something" CommandArgument='<%#Eval("id_p") %>'
Text="Stavi u košaricu" />
</ItemTemplate>
</asp:TemplateField>
Now you handle the RowCommand Event:
protected void GridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
string m_id = e.CommandArgument.ToString();
if (e.CommandName == "Something")
{
//do something with m_id
}
}
I've spent all afternoon trying to do this using CODE BEHIND without success so I am asking for some C# code.
Basically, I have a GV and a DV in a master/detail relationship. GV displays ID and Name. If I click Select on a GV row, I want to see its ID, Name and Address in DV. I know how get this to work declaratively in an aspx file. But in C# code behind, I don't know how to proceed at this function:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Select"))
{
// PLEASE GIVE ME THE CODE HERE TO BIND THE DETAILSVIEW. THANKS!
// I am using a sqldatasource if it makes any difference
}
}
Here's a general solution showing you how to achieve this, please note that this solution isn't extremely error-safe but I suppose you'll get the jist of it. Please comment if there's anything unclear.
Code-behind:
protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
GridViewRow selected = gv.Rows[Convert.ToInt32(e.CommandArgument)];
List<ThatClass> cList = new List<ThatClass>();
cList.Add(new ThatClass(selected.Cells[0].Text, selected.Cells[1].Text));
dv.DataSource = cList;
dv.DataBind();
}
}
Markup:
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false" OnRowCommand="gv_RowCommand">
<Columns>
<asp:BoundField DataField="A" HeaderText="A"/>
<asp:BoundField DataField="B" HeaderText="B" />
<asp:CommandField ShowSelectButton="true" />
</Columns>
</asp:GridView>
<asp:DetailsView runat="server" ID="dv">
</asp:DetailsView>
FYI: I bound the GV using a List:
protected void Page_Load(object sender, EventArgs e)
{
List<ThatClass> cList = new List<ThatClass>();
cList.Add(new ThatClass("123", "abc"));
cList.Add(new ThatClass("456", "def"));
gv.DataSource = cList;
gv.DataBind();
}