Updating GridView with DataSet source
I am working on a page to let a user import an excel file into our database. I want to allow the user to manipulate certain fields before committing the information so I loaded a DataSet from the Excel file and bound that to a GridView. For some reason on rowUpdate my NewValues collection is empty.
Here is my GridView:
<cc1:WWGridView ID="gvImportDisplay" runat="server" CssClass="grid-view" AllowSorting="false" ShowHeader="true"
AutoGenerateColumns="true"
AutoGenerateEditButton="true"
OnRowEditing="gvImportDisplay_RowEditing"
OnRowCance开发者_StackOverflowlingEdit="gvImportDisplay_RowCancelingEdit"
OnRowUpdating="gvImportDisplay_RowUpdating"
>
<EmptyDataTemplate>0 results found. </EmptyDataTemplate>
</cc1:WWGridView>
And here is my code behind for the update:
protected void gvImportDisplay_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
foreach (DictionaryEntry entry in e.NewValues)
{
string headerText = entry.Key.ToString();
if (string.IsNullOrEmpty(headerText))
continue;
string newValue = entry.Value.ToString();
if (excelDataSet.Tables["ExcelInfo"].Rows[gvImportDisplay.EditIndex][headerText] == null)
continue;
excelDataSet.Tables["ExcelInfo"].Rows[gvImportDisplay.EditIndex][headerText] = newValue;
}
gvImportDisplay.EditIndex = -1;
RefreshGridView();
}
"excelDataSet" is a property of the page that gets saved in the session between postbacks.
When I step through the code the foreach gets skipped completely because e.NewValues.Count = 0.
Can anyone see why the NewValues would be empty? Do I need to rebind the GridView before doing the update? Or perhaps because I don't have a DataKey it's not working? There is no column I can use as a DataKey because I can't guarantee that any particular column is unique.
I guess two way databinding doesn't work in this case. You should wrap the data set in ObjectDataSource and set this data source using GridView.DataSourceID
example:
http://msdn.microsoft.com/en-us/library/1se6685s.aspx
e.NewValues still is not working, I'm guessing because I don't have DataKeys.
I was able to get an update working by manually defining the columns and doing a FindControl to get the new values. Since I am going to have to manually define the columns anyway for another aspect of the grid view I am going to go with that.
精彩评论