First, I am using v.8.3 and coding in C#.
I am trying to get a simple grid bound to a SQLDataSource to update using only (as much as is possible) C# code in the .cs file.
I would like to be able to define my Parameters for the update query of the SQLDatasource, bind it to the grid and have it update with UpdateRowBatch (on a per row basis).
I have searched far and wide for example code of setting the parameters via C# code, but have only found examples using the gui wizard to create the CRUD when defining the SQLDataSource.
The data displays fine, but does not update. Any help is greatly appreciated.
Here is the code I am trying to use. Please let me know if I need to provide any additional information.
I have tried to minimize the code, but there still may be some extra stuff in there.
.aspx code:
<form id="form1" runat="server">
<div>
<igtbl:UltraWebGrid ID="wgData" runat="server" DisplayLayout-AutoGenerateColumns="true"
DataSourceID="mySqlDS"
DataKeyField="myTestID"
DataMember="DefaultView"
Height="300px" Width="525px"
OnInitializeLayout="wgData_InitializeLayout"
OnInitializeRow="wgData_InitializeRow"
OnUpdateRowBatch="wgData_UpdateRowBatch">
<DisplayLayout AllowSortingDefault="Yes" BorderCollapseDefault="Separate" HeaderClickActionDefault="SortMulti" Name="wgData" RowHeightDefault="40px" Version="4.00">
<FrameStyle Height="300px" Width="525px"></FrameStyle>
<AddNewBox Hidden="False"></AddNewBox>
</DisplayLayout>
</igtbl:UltraWebGrid>
</div>
<asp:SqlDataSource ID="mySqlDS" runat="server" ConnectionString="<%$ ConnectionStrings:myConnectionString %>"></asp:SqlDataSource>
<asp:Button ID="btnSave" runat="server" CausesValidation="False" Text="Save" />
</form>
and the .cs code:
public partial class myTestForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
if (!this.IsPostBack)
this.wgData.DataBind();
}
wgData.DataSourceID = mySqlDS.UniqueID;
wgData.Bands[0].DataKeyField = "myTestID";
mySqlDS.OldValuesParameterFormatString = "original_{0}";
Parameter original_myTestID = new Parameter("original_myTestID", TypeCode.Int32);
Parameter myTestID = new Parameter("myTestID", TypeCode.Int32);
Parameter myTestCheckFlag = new Parameter("myTestCheckFlag", TypeCode.Boolean);
Parameter myTestCode = new Parameter("myTestCode", TypeCode.String);
Parameter myTestDesc = new Parameter("myTestDesc", TypeCode.String);
mySqlDS.UpdateParameters.Add(original_myTestID);
mySqlDS.UpdateParameters.Add(myTestCheckFlag);
mySqlDS.UpdateParameters.Add(myTestCode);
mySqlDS.UpdateParameters.Add(myTestDesc);
mySqlDS.InsertParameters.Add(myTestID);
mySqlDS.InsertParameters.Add(myTestCode);
mySqlDS.InsertParameters.Add(myTestDesc);
mySqlDS.InsertParameters.Add(myTestCheckFlag);
mySqlDS.SelectCommand = "SELECT * FROM [myTestTable]";
mySqlDS.UpdateCommand = "UPDATE [myTestTable] SET [myTestDesc] = @myTestDesc, [myTestCode] = @myTestCode, [myTestCheckFlag] = @myTestCheckFlag WHERE [myTestID] = @original_myTestID";
protected void wgData_InitializeLayout(object sender, Infragistics.WebUI.UltraWebGrid.LayoutEventArgs e)
e.Layout.AllowUpdateDefault = Infragistics.WebUI.UltraWebGrid.AllowUpdate.Yes;
e.Layout.AllowAddNewDefault = Infragistics.WebUI.UltraWebGrid.AllowAddNew.Yes;
e.Layout.AddNewRowDefault.Visible = Infragistics.WebUI.UltraWebGrid.AddNewRowVisible.Yes;
e.Layout.Bands[0].Columns.FromKey("myTestID").BaseColumnName = "myTestID";
e.Layout.Bands[0].Columns.FromKey("myTestDesc").BaseColumnName = "myTestDesc";
e.Layout.Bands[0].Columns.FromKey("myTestCode").BaseColumnName = "myTestCode";
e.Layout.Bands[0].Columns.FromKey("myTestCheckFlag").BaseColumnName = "myTestCheckFlag";
e.Layout.Bands[0].Columns.FromKey("myTestID").Key = "myTestID";
e.Layout.Bands[0].Columns.FromKey("myTestDesc").Key = "myTestDesc";
e.Layout.Bands[0].Columns.FromKey("myTestCode").Key = "myTestCode";
e.Layout.Bands[0].Columns.FromKey("myTestCheckFlag").Key = "myTestCheckFlag";
e.Layout.Bands[0].Columns.FromKey("myTestID").DataType = "System.Int32";
e.Layout.Bands[0].Columns.FromKey("myTestDesc").DataType = "System.String";
e.Layout.Bands[0].Columns.FromKey("myTestCode").DataType = "System.String";
e.Layout.Bands[0].Columns.FromKey("myTestCheckFlag").DataType = "System.Boolean";
protected void wgData_InitializeRow(object sender, Infragistics.WebUI.UltraWebGrid.RowEventArgs e)
protected void wgData_UpdateRowBatch(object sender, Infragistics.WebUI.UltraWebGrid.RowEventArgs e)
Thank you.
Can you please elaborate on what you mean by "You can also use the DataBinding event of the WebGrid to ensure the DataSource is proprerly initialized prior to the grid DataBinding". How can a DataSource not be 'properly' initialized ?
That's odd that it wouldn't compile. What was the compilation error? I'm guessing "mySqlDS" is undefined? That's the only reason I could see that code not compiling. If mySqlDS is inside of a container control, you may need to use "FindControl" to gain a reference to it. Generally I recommend initialization be done inside of the InitializeDataSource event of the WebGrid.
Thank you Tony!
Moving the Parameter code for my DataSource into the DataSources Init event made things work.
I tried the Databindinng event of the Grid to set wgData.DataSourceID = mySqlDS.UniqueID; but it would not compile.
What type of code (an example setting or two would be most useful) do you suggest putting in the grid's DataBinding event?
Thank again... Jonathan
The grid will fire it's Update event's before the Page_Load is fired, which is probably why your updates are failing. It's better to use the OnInit event of the DataSource to initialize the UpdateParameters. You can also use the DataBinding event of the WebGrid to ensure the DataSource is proprerly initialized prior to the grid DataBinding. Updating is a direct result of the grid databinding, so if it doesn't have the appropriate parameter information at DataBind, your updates won't automatically happen.
Hope this helps,
-Tony