This is a migrated thread and some comments may be shown as answers.

[Solved] Grid for updating DB table

1 Answer 130 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jimmy
Top achievements
Rank 1
Jimmy asked on 22 Apr 2009, 10:28 AM

Hi,

I am trying to get RadGrid to work. What I want to achive is simply a Grid that lets me modify the content of a database table. However, most online samples using the Grid Control is based on ASP.NET - I'm using the Grid Control within a SharePoint WebPart, and therefore needs to do it all programmatically (C# .NET)

When I execute my WebPart, I receive no errors. The Grid simply dosn't render. My code follows below. Any ideas?

 - Thanks in advance.

Regards
Jimmy Thomsen

=============================================================================

namespace CompGrid
{
    public class Grid : WebPart
    {
        private ScriptManager sm;
        private RadGrid grid;

        protected override void OnLoad(EventArgs e)
        {
            this.EnsureChildControls();
        }

        protected override void CreateChildControls()
        {
            if (ScriptManager.GetCurrent(this.Page) != null)
            {
                this.sm = ScriptManager.GetCurrent(this.Page);
            }
            else
            {
                this.sm = new ScriptManager();
                this.Controls.Add(this.sm);
            }

            this.grid = new RadGrid();
            this.Controls.Add(this.grid);

            this.grid.AllowAutomaticDeletes = true;
            this.grid.AllowAutomaticInserts = true;
            this.grid.AllowAutomaticUpdates = true;
            this.grid.AllowMultiRowEdit = true;
            this.grid.AutoGenerateColumns = true;
            this.grid.AutoGenerateDeleteColumn = true;
            this.grid.AutoGenerateEditColumn = true;

            if (this.Page.IsPostBack == false)
            {
                SqlDataSource dataSource = new SqlDataSource("Data Source=Comp-demo;Initial Catalog=TestDB;User ID=admin;Password=test", "SELECT xaxis, series1, series2 FROM testdata");
                dataSource.UpdateCommand = "UPDATE testdata SET xaxis=@xaxis, series1=@series1, series2=@series2 WHERE id=@id";
                dataSource.DeleteCommand = "DELETE FROM testdata WHERE id=@id";
                dataSource.InsertCommand = "INSERT INTO testdata VALUES (xaxis=@xaxis, series1=@series1, series2=@series2)";
               
                this.grid.DataSourceID = dataSource.ID;
            }
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            this.sm.RenderControl(writer);
            this.grid.RenderControl(writer);
        }
    }
}

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 22 Apr 2009, 11:15 AM
Hello Jimmy,

Can you either try assigning the Id for the DataSource declaratively first and then assign the datasource to the grid or assign the datasource directly as shown below. Probably this might help.

c#:
  SqlDataSource dataSource = new SqlDataSource("Data Source=Comp-demo;Initial Catalog=TestDB;User ID=admin;Password=test""SELECT xaxis, series1, series2 FROM testdata"); 
  dataSource.ID = "SqlDataSource1"
  this.grid.DataSourceID = "SqlDataSource1"
 
 (OR) 
 
  SqlDataSource dataSource = new SqlDataSource("Data Source=Comp-demo;Initial Catalog=TestDB;User ID=admin;Password=test""SELECT xaxis, series1, series2 FROM testdata");  
  this.grid.DataSource = dataSource; 

Thanks
Princy.
Tags
Grid
Asked by
Jimmy
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or