DataGrid Pagin Problem

G

Guest

Hi there
my problem is with datagrid pagin.when i try to add the pagin feature to my
datagrid i get a strange behavior.
i will explain in details:
i wanted to allow pagin in the datagrid so i have put AllowPagin property =
true;
and then i have implemented the PageIndexChanged event handler.and made all
required changes to re-filling the datasource.in this case the pagin worked
fine.
but when i have put the AutoGenerateColumns property = false the pagin
didn't work.i don't know why this happened.i hopw that you can help me
Thanks
this is my current code:
////////////
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!IsPostBack)
{
MyDataGridBind();
}
}
//Start of the MyDataGridBind() Function//
void MyDataGridBind()
{
SqlConnection sqlcon = new SqlConnection();
SqlCommand sqlcom = new SqlCommand();
SqlDataAdapter sqlDA = new SqlDataAdapter();
DataSet dsRates = new DataSet();

sqlcon.ConnectionString =
ConfigurationSettings.AppSettings["connectionString"];
sqlcom.Connection = sqlcon;
sqlcom.CommandType = CommandType.Text;
sqlcom.CommandText = "SELECT * FROM rates";
sqlDA.SelectCommand = sqlcom;
sqlDA.Fill(dsRates);
DataGrid1.DataSource = dsRates;

BoundColumn bc2 = new BoundColumn();
BoundColumn bc3 = new BoundColumn();
ButtonColumn bc5 = new ButtonColumn();

bc2.DataField = "country";
bc2.HeaderText = "Fiyat";
DataGrid1.Columns.Add(bc2);

bc3.DataField = "rate";
bc3.HeaderText = "Bilgi";
DataGrid1.Columns.Add(bc3);

bc5.ButtonType = ButtonColumnType.LinkButton;
bc5.CommandName = "SendMail";
bc5.HeaderText = "BaÅŸvur";
bc5.Text = "Satın al";
DataGrid1.Columns.Add(bc5);

DataGrid1.DataBind();
}
//End of the MyDataGridBind() Function//

private void DataGrid1_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
MyDataGridBind();
}
 
G

Guest

When you add the columns dynamically during the data bind (as you did below)
then you should redo the data binding upon each post back, i.e. if you remove
the condition: if (!IsPostBack) your code will work.

Otherwise, if you know before hand the number of columns to add to the grid
you can use the declarative syntaxand then your code would work just as good:

<Columns>
<asp:BoundColumn HeaderText="Fiyat" DataField="Country" ></asp:BoundColumn>
<asp:BoundColumn HeaderText="Rate" DataField="Rate" ></asp:BoundColumn>
</Columns>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top