Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
C#
using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;

namespace CustomRaghav.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {
        HiddenField HFSN = new HiddenField();
        DataTable table = new DataTable();
        DataTable result = new DataTable();
        private int srno = 1;
        private string headlines;
        private string date;
        Button btn = new Button();

        private const string _ascxPath = @"~/_CONTROLTEMPLATES/GridViewWP/GridView8/VisualWebPart1UserControl.ascx";
        private Microsoft.SharePoint.WebControls.SPGridView _grid;
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected override void CreateChildControls()
        {
            table = GetDataTable();
            btn.Visible = true;
            btn.Text = "Show";
            btn.Click += new EventHandler(btn_Click);
            _grid = new Microsoft.SharePoint.WebControls.SPGridView();
            _grid.AutoGenerateColumns = false;
            _grid.AllowPaging = true;
            _grid.PageSize = 5;
            //_grid.PageIndexChanging += _grid_PageIndexChanging;
            foreach (DataColumn column in table.Columns)
                _grid.Columns.Add(new BoundField()
                {
                    DataField = column.ColumnName,
                    HeaderText = column.ColumnName
                });
            //_grid.DataSource = table;
            this.Controls.Add(_grid);
            // _grid.PagerTemplate = null;
            _grid.DataBind();
            _grid.Visible = true;
            this.Controls.Add(btn);
        }


        void btn_Click(object sender, EventArgs e)
        {

            //srno = Convert.ToInt32(HFSN.Value);
            DataRow[] rows = table.Select();
            if (srno <= rows.Length)
            {
                for (int i = 0; i < rows.Length; i++)
                {

                    if (rows[i]["srno"].ToString() == srno.ToString())
                    {

                        headlines = rows[i]["headlines"].ToString();
                        date = rows[i]["date"].ToString();
                    }

                }

                if ((srno) == 1)
                {
                    result.Columns.Add("srno", typeof(int));
                    result.Columns.Add("date", typeof(string));
                    result.Columns.Add("Headlines", typeof(string));
                }
                result.Rows.Add(new object[] { srno, date, headlines });
            }

            srno++;
            //HFSN.Value =Convert.ToString(srno);

            _grid.DataSource = result;
            _grid.DataBind();
            // DataTable table = GetDataTable();

            _grid.Visible = true;


        }



        private DataTable GetDataTable()
        {
            //  DataTable result = new DataTable();

            table.Columns.Add("srno", typeof(int));
            table.Columns.Add("headlines", typeof(string));
            table.Columns.Add("date", typeof(System.DateTime));
            //for (int i = 1; i <= 5; i++)
            table.Rows.Add(new object[] { 1, "Chinese Troops Begin Withdrawing from Ladakh Hours after Xi-Modi meet", "2014-09-22" });
            table.Rows.Add(new object[] { 2, "BJP-Shiv Sena Alliance of 25 years on verge of Collapse", "2014-09-21" });
            table.Rows.Add(new object[] { 3, "Meet THe Highest Female Executive In the World", "2014-09-22" });
            table.Rows.Add(new object[] { 4, "Indian Muslims will Live And Die For India,PM Says", "2014-09-21" });
            table.Rows.Add(new object[] { 5, "Two Doctors, Who got stuck in J&K floods, turn saviours for this town", "2014-09-21" });
            return table;


        }
    }
}
Posted
Updated 22-Sep-14 4:23am
v2

1 solution

Do not assign a value while declaring your variable

Change
C#
private int srno = 1;

to
C#
private int srno;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900