Binding RadGrid To DataTable ASP.NET - c#

What i am trying to do is that before saving records to database i want to show the records in GridView that is when user fill the text box and click on button the record is to be shown on GridView with serial Number 1 , 2 , 3 ...
So far i have approach this . The first records get added successfully but when adding second record the DataTable give null reference exception . Is this the right approach ? Or is there an easy way to do this . I am using Telerik RadGrid for asp.net .
public DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dt = new DataTable();
dt.Columns.Add("Sn#");
dt.Columns.Add("type");
dt.Columns.Add("AccountTitle");
dt.Columns.Add("Description");
dt.Columns.Add("CostCenter");
dt.Columns.Add("Debit");
dt.Columns.Add("Credit");
sno.Text = "1";
}
}
protected void radGridView2_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
dt = AddRow(dt); // call the method to create row
ViewState["dt"] = dt;
dt = (DataTable)ViewState["dt"];
radGridView2.DataSource = dt;
}
private DataTable AddRow(DataTable dt)
{ // method to create row
DataRow dr = dt.NewRow();
dr[0] = sno.Text;
dr[1] = type.Text;
dr[2] = htitle.Text;
dr[3] = disc.Text;
dr[4] = job.SelectedItem.Text;
dr[5] = drr.Text;
dr[6] = crr.Text;
dt.Rows.Add(dr);
return dt;
}
protected void b1_Click(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["dt"];
ViewState["dt"] = AddRow(dt);
radGridView2.Rebind();
Session["cttt"] = Convert.ToInt32(sno.Text)+1;
sno.Text = Session["cttt"].ToString();
}

I have copy your code and change a bit. Due to your dt declared twice. I believe your error will be the DataTable is null
.aspx
<asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
<div>
<telerik:RadGrid ID="rg" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource"></telerik:RadGrid>
<br />
<asp:TextBox ID="sno" runat="server"></asp:TextBox><br />
<asp:TextBox ID="type" runat="server" Text="A"></asp:TextBox><br />
<asp:TextBox ID="htitle" runat="server" Text="B"></asp:TextBox><br />
<asp:TextBox ID="disc" runat="server" Text="C"></asp:TextBox><br />
<asp:DropDownList ID="job" runat="server"><asp:ListItem>Yeah</asp:ListItem></asp:DropDownList><br />
<asp:TextBox ID="drr" runat="server" Text="D"></asp:TextBox><br />
<asp:TextBox ID="crr" runat="server" Text="E"></asp:TextBox><br />
<br />
<br />
<asp:Button ID="b1" runat="server" Text="Click" OnClick="b1_Click" />
</div>
</asp:ScriptManager>
.cs
private DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
sno.Text = "1";
}
}
private DataTable CreateDataTable()
{
dt = new DataTable();
dt.Columns.Add("Sn#");
dt.Columns.Add("type");
dt.Columns.Add("AccountTitle");
dt.Columns.Add("Description");
dt.Columns.Add("CostCenter");
dt.Columns.Add("Debit");
dt.Columns.Add("Credit");
return dt;
}
private DataTable AddRow(DataTable dt1)
{ // method to create row
DataRow dr = dt1.NewRow();
dr[0] = sno.Text;
dr[1] = type.Text;
dr[2] = htitle.Text;
dr[3] = disc.Text;
dr[4] = job.SelectedItem.Text;
dr[5] = drr.Text;
dr[6] = crr.Text;
dt1.Rows.Add(dr);
return dt1;
}
protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
rg.DataSource = ViewState["dt"] as DataTable;
}
protected void b1_Click(object sender, EventArgs e)
{
DataTable dt1 = ViewState["dt"] != null ? ViewState["dt"] as DataTable : CreateDataTable();
ViewState["dt"] = AddRow(dt1);
rg.Rebind();
Session["cttt"] = Convert.ToInt32(sno.Text) + 1;
sno.Text = Session["cttt"].ToString();
}
Result

You are creating a variable with the same name as the global variable (dt) in the event b1_click - this is causing the confusion and the error.
protected void b1_Click(object sender, EventArgs e)
{
//Use the global variable dt
//DataTable dt = (DataTable)ViewState["dt"];
dt = (DataTable)ViewState["dt"];
ViewState["dt"] = AddRow(dt);
radGridView2.Rebind(); //Note this would call the NeedDataSource event
Session["cttt"] = Convert.ToInt32(sno.Text)+1;
sno.Text = Session["cttt"].ToString();
}
Another point to note here is that the AddRow method will be called twice on the button click. Once from b1_click event and then again from NeedDataSource event (which gets triggered on radGridView2.Rebind())

Related

how can i remove the gridview column in asp.net c#?

I have a gridview and output generated is
But I want to remove the Column_Name column and want this type of output
Never change SQL query because in this way the query works according to my logic. And I also want to remove &nbsp in every textbox and how can I get all the gridview data by using loop and save in SQL Server database?
ASPX:
<asp:Content ID="Content2" ContentPlaceHolderID="body" Runat="Server">
<form id="form" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"
onrowdatabound="GridView1_RowDataBound">
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="load" Text="gridview" />
<asp:Button ID="Button2" runat="server" OnClick="show" Text="Button" />
<asp:Button ID="Button3" runat="server" OnClick="save" Text="Save" />
</form>
</asp:Content>
Codebehind:
SqlConnection cnn = new SqlConnection("Data Source=LIFE_WELL;Initial Catalog=db_compiler;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
string query = "USE db_compiler SELECT Column_Name FROM tbl_field WHERE Table_Name='deptt'";
SqlCommand cmd = new SqlCommand(query, cnn);
DataTable dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable table = new DataTable();
adp.Fill(dt);
Session["num"] = dt.Rows.Count;
for (int i = 0; i < dt.Rows.Count; i++)
{
DataColumn dc = new DataColumn(dt.Rows[i]["Column_Name"].ToString());
dt.Columns.Add(dc);
}
DataRow r = table.NewRow();
GridView1.DataSource = table;
GridView1.DataBind();
cnn.Close();
cnn.Open();
cmd.ExecuteNonQuery();
cnn.Close();
Session["table"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
TextBox txt = new TextBox();
txt.Text = e.Row.Cells[i].Text;
e.Row.Cells[i].Text = "";
e.Row.Cells[i].Controls.Add(txt);
}
}
}
Change Your Codebehind code:
protected void Page_Load(object sender, EventArgs e)
{
If(!IsPostBack)
{
string query = "USE db_compiler SELECT Column_Name FROM tbl_field WHERE Table_Name='deptt'";
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
DataTable dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable table = new DataTable();
adp.Fill(dt);
Session["num"] = dt.Rows.Count;
for (int i = 0; i < dt.Rows.Count; i++)
{
DataColumn dc = new DataColumn(dt.Rows[i]["Name"].ToString());
table.Columns.Add(dc); // Make this Column in 2nd DataTable and bind that to Gridview
}
DataRow r = table.NewRow();
Session["table"] = dt;
table.Rows.Add(r); // Add as many row you want to add with for loop
Gridview1.DataSource = table;
Gridview1.DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
TextBox txt = new TextBox();
e.Row.Cells[i].Controls.Add(txt);
}
}
}
You can try with this modified RowDataBound event handler:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 1; i < e.Row.Cells.Count; i++) // Does not process the first cell
{
TextBox txt = new TextBox();
TableCell cell = e.Row.Cells[i];
txt.Text = cell.Text.Replace(" ", ""); // Removes
cell.Text = "";
cell.Controls.Add(txt);
}
}
e.Row.Cells.RemoveAt(0); // Removes the first cell in the row
}

Preserve textbox values inside a gridview on button click

I have an editable GridView, which has a AddRow button, on click of which an empty row consisting of text boxes is added.
Whenever I insert values inside these textboxes, and click AddRow or Delete button, the text boxes loose their values.
I am sure, this doesn't happen due to postback, because there's a Save button too, on the click of which the values are normally preserved.
Below is the markup:
<asp:GridView ID="GridView1" runat="server" ShowFooter="true" AutoGenerateColumns="false"
OnRowDeleting="GridView1_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="Col1">
<ItemTemplate>
<asp:TextBox runat="server" ID="txt1"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Col2">
<ItemTemplate>
<asp:TextBox ID="txt2" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Col3">
<ItemTemplate>
<asp:TextBox ID="txt3" runat="server"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="btnAddNewRow" runat="server" Text="AddRow" OnClick="Add" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Button" ShowDeleteButton="true" />
</Columns>
</asp:GridView>
<asp:Button ID="btnSavetoDB" runat="server" Text="save" OnClick="btnSavetoDB_Click" />
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SetInitialRow();
}
}
//Sets the first empty row to the grid view
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
dr = dt.NewRow();
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dr["Column3"] = string.Empty;
dt.Rows.Add(dr);
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
//Adds a new empty row to the grid view
protected void Add(object sender, EventArgs e)
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 0; i < dtCurrentTable.Rows.Count; i++)
{
//extract the TextBox values
TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[0].FindControl("txt1");
TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("txt2");
TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("txt3");
drCurrentRow = dtCurrentTable.NewRow();
//drCurrentRow["RowNumber"] = i + 1;
drCurrentRow["Column1"] = box1.Text;
drCurrentRow["Column2"] = box2.Text;
drCurrentRow["Column3"] = box3.Text;
rowIndex++;
}
//add new row to DataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//Store the current data to ViewState
ViewState["CurrentTable"] = dtCurrentTable;
//Rebind the Grid with the current data
GridView1.DataSource = dtCurrentTable;
GridView1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
}
//Called on Delete Button click, which is inside a CommanField
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int num = GridView1.Rows.Count;
int index = Convert.ToInt32(e.RowIndex);
if (num > 1)
{
DataTable dt = ViewState["CurrentTable"] as DataTable;
dt.Rows[index].Delete();
ViewState["CurrentTable"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
Initial snapshot:
After AddRow button click:
Where am I going wrong?
Experts please help.
Regards
The problem is that you haven't used Eval in your markup
<asp:TextBox runat="server" ID="txt1" Text='<%# Eval("Column1") %>'></asp:TextBox>
Optimized code-behind. No need of ViewState
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var table = CreateDataTable();
table.Rows.Add("", "", "");
BindGridView(table);
}
}
//Sets the first empty row to the grid view
private DataTable CreateDataTable()
{
var dt = new DataTable
{
Columns = { "Column1", "Column2", "Column3" }
};
return dt;
}
private void BindGridView(DataTable table)
{
GridView1.DataSource = table;
GridView1.DataBind();
}
private DataTable PopulateTableFromGridView()
{
var table = CreateDataTable();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
//extract the TextBox values
TextBox box1 = (TextBox)GridView1.Rows[i].FindControl("txt1");
TextBox box2 = (TextBox)GridView1.Rows[i].FindControl("txt2");
TextBox box3 = (TextBox)GridView1.Rows[i].FindControl("txt3");
table.Rows.Add(box1.Text, box2.Text, box3.Text);
}
return table;
}
//Adds a new empty row to the grid view
protected void Add(object sender, EventArgs e)
{
var newTable = PopulateTableFromGridView();
newTable.Rows.Add("", "", "");
BindGridView(newTable);
}
//Called on Delete Button click, which is inside a CommanField
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
var dt = PopulateTableFromGridView();
dt.Rows[e.RowIndex].Delete();
if (dt.Rows.Count == 0)
{
dt.Rows.Add("", "", "");
}
BindGridView(dt);
}
Links on inline expressions
http://support.microsoft.com/kb/976112
http://weblogs.asp.net/ahmedmoosa/embedded-code-and-inline-server-tags

how to delete from gridview without database "check to determine if the object is null before calling the method"

The ff are codes in my .cs file
private void BindGridview(int rowcount)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("Code", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("Course", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("Credit", typeof(String)));
if (ViewState["CurrentData"] != null)
{
for (int i = 0; i < rowcount + 1; i++)
{
dt = (DataTable)ViewState["CurrentData"];
if (dt.Rows.Count > 0)
{
dr = dt.NewRow();
dr[0] = dt.Rows[0][0].ToString();
}
}
dr = dt.NewRow();
dr[0] = this.cboCourseCode.Text;
dr[1] = this.txtCourseName.Text;
dr[2] = this.txtCredit.Text;
dt.Rows.Add(dr);
}
else
{
dr = dt.NewRow();
dr[0] = this.cboCourseCode.Text;
dr[1] = this.txtCourseName.Text;
dr[2] = this.txtCredit.Text;
dt.Rows.Add(dr);
}
// If ViewState has a data then use the value as the DataSource
if (ViewState["CurrentData"] != null)
{
GridView1.DataSource = (DataTable)ViewState["CurrentData"];
GridView1.DataBind();
}
else
{
// Bind GridView with the initial data assocaited in the DataTable
GridView1.DataSource = dt;
GridView1.DataBind();
}
// Store the DataTable in ViewState to retain the values
ViewState["CurrentData"] = dt;
}
protected void BindGrid()
{
GridView1.DataSource = ViewState["dt"] as DataTable;
GridView1.DataBind();
}
protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
int index = Convert.ToInt32(e.RowIndex);
DataTable dt = ViewState["dt"] as DataTable;
dt.Rows[e.RowIndex].Delete();
ViewState["dt"] = dt;
BindGrid();
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string Code = e.Row.Cells[0].Text;
foreach (Button button in e.Row.Cells[3].Controls.OfType<Button>())
{
if (button.CommandName == "Delete")
{
button.Attributes["onclick"] = "if(!confirm('Do you want to delete " + Code + "?')){ return false; };";
}
}
}
}
protected void cboCourseCode_SelectedIndexChanged(object sender, EventArgs e)
{
this.Populate_Course_Details();
}
protected void BtnAdd_Click(object sender, EventArgs e)
{
/* DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Code"), new DataColumn("Course"), new DataColumn("Credit") });
dt.Rows.Add(this.cboCourseCode.Text, this.txtCourseName.Text, this.txtCredit.Text);
ViewState["dt"] = dt;
BindGrid();*/
if (ViewState["CurrentData"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentData"];
int count = dt.Rows.Count;
BindGridview(count);
}
else
{
BindGridview(1);
}
}
My aspx file also has this gridview
<asp:GridView ID="GridView1" CssClass = "Grid" runat="server" OnRowDeleting="OnRowDeleting" AutoGenerateColumns = "false" OnRowDataBound = "OnRowDataBound">
<Columns>
<asp:BoundField DataField="Code" HeaderText="Code" />
<asp:BoundField DataField="Course" HeaderText="Course" />
<asp:BoundField DataField="Credit" HeaderText="Credit" />
<asp:CommandField ShowDeleteButton="True" ButtonType="Button" />
</Columns>
</asp:GridView>
When I start to delete, I get this error:
check to determine if the object is null before calling the method
Error Reason
in OnRowDeleting function you are creating datatable and assigning viewstate["dt"],which doesn't have current data. that's why it shows Object is Null
Solution
Try This
First make one of the column as data key.consider i want to make "Code" column as datakey.
datakey will help to find specific row to delete
<asp:GridView ID="GridView1" CssClass = "Grid" runat="server" OnRowDeleting="OnRowDeleting" AutoGenerateColumns = "false" OnRowDataBound = "OnRowDataBound" DataKeyNames="Code">
After That in .cs code
create a datatable with currentdata
make "Code" column as primary key
find particular code(which you want to delete)using find()
then delete that row using delete() function
Reflect the deletion in viewsate["currentdata"]
then bind to gridview
protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
string code = GridView1.DataKeys[e.RowIndex].Value.ToString();
if (ViewState["CurrentData"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentData"];
dt.PrimaryKey = new DataColumn[] { dt.Columns["Code"] };
dt.Rows.Find(code).Delete();
ViewState["CurrentData"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
}

Access gridview rows after postback

Good day!
I need to dynamycally upload files and display information in gridview.
After file upload, i need to select file type in dropdown.
But after postback i can't access Gridview1 rows, and get selected file types. After postback Gridview1.Rows.Count = 0.
Is it possible to get selected values from DropDownLists?
<asp:GridView ID="GridView1" runat="server" ShowHeader="False" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="FileName" />
<asp:TemplateField HeaderText="FileType">
<ItemTemplate>
<asp:DropDownList runat="server">
<asp:ListItem Value="Val1">Val1</asp:ListItem>
<asp:ListItem Value="Val2">Val2</asp:ListItem>
<asp:ListItem Value="Val3">Val3</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField DeleteText="Remove" ShowDeleteButton="true" />
</Columns>
</asp:GridView>
<asp:FileUpload ID="FileUpload" runat="server" onchange="this.form.submit()" />
Thanks
In Page_Load, during PostBack, GridView rows is empty.
protected void Page_Load(object sender, EventArgs e)
{
RestoreForm();
if (IsPostBack && FileUpload.HasFile)
{
AddRow(FileUpload.PostedFile.FileName);
}
FilesGridView.RowDeleting += new GridViewDeleteEventHandler(RemoveFileFromTable);
}
private void AddRow(string file)
{
DataTable dt = (DataTable)Page.Session["Files"];
if (dt == null)
{
AddDataTableToSession();
dt = (DataTable)Page.Session["Files"];
}
DataRow dr = dt.NewRow();
dr["FileName"] = file;
dr["FileType"] = 0;
dt.Rows.Add(dr);
Page.Session["Files"] = dt;
FilesGridView.DataSource = dt;
FilesGridView.DataBind();
}
private void AddDataTableToSession()
{
DataTable dt = new DataTable("Files");
DataColumn dc = new DataColumn("FileName", Type.GetType("System.String"));
dt.Columns.Add(dc);
dc = new DataColumn("FileType", Type.GetType("System.String"));
dt.Columns.Add(dc);
Page.Session["Files"] = dt;
}
private void RemoveFileFromTable(object sender, GridViewDeleteEventArgs e)
{
int recordToDelete = e.RowIndex;
DataTable dt = (DataTable)Page.Session["Files"];
int cn = dt.Rows.Count;
dt.Rows.RemoveAt(recordToDelete);
dt.AcceptChanges();
Page.Session["Files"] = dt;
FilesGridView.DataSource = dt;
FilesGridView.DataBind();
}
Try by changing Ispostback like this.
if (!IsPostBack && FileUpload.HasFile)
{
AddRow(FileUpload.PostedFile.FileName);
}
(or)
if (IsPostBack == false && FileUpload.HasFile)
{
AddRow(FileUpload.PostedFile.FileName);
}
So that, if page load occurs, Your if condition will get true.
You can find the drop down list and update it with the value in the RowDataBOund event of gridview as follows
protected void Page_Load(object sender, EventArgs e)
{
//RestoreForm();
if (IsPostBack && FileUpload.HasFile)
{
AddRow(FileUpload.PostedFile.FileName);
}
else
{
AddDataTableToSession();
}
FilesGridView.RowDeleting += new GridViewDeleteEventHandler(RemoveFileFromTable);
FilesGridView.RowDataBound += KBFilesGridView_RowDataBound;
}
and row databound will be as follows
void KBFilesGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList ddl = e.Row.FindControl("DropDownList1") as DropDownList;
if (ddl != null)
{
DataRow dr= ((DataRowView)e.Row.DataItem).Row;
ddl.SelectedValue = dr["FileType"].ToString();
}
}
Similary you can get the value of dropdown for remove method as well as follows
private void RemoveFileFromTable(object sender, GridViewDeleteEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = e.Row.FindControl("DropDownList1") as DropDownList;
if (ddl != null)
{
if(ddl.SelectedValue == "someValue") doSomeThing();
}
}
}

BatchEditSettings.ShowConfirmOnLosingChanges not working

I am developing a GridView that allow editing. I want to disable the warning message of data loss. However its not working.
For example, I edit some field without click the "Save Changes" and then click the column header for sorting, the warning message "Are you sure you want to perform the action? All unsaved grid data will be lost." still alert.
How could I solve this problem?
Here are my sample codes:
TestingWeb.aspx
<form id="form1" runat="server">
<dx:ASPxGridView ID="ASPxGridView1" runat="server" KeyFieldName="Line" OnRowUpdating="ASPxGridView1_RowUpdating">
<Columns>
<dx:GridViewDataTextColumn Caption="Line" FieldName="Line" Name="col_Line" ReadOnly="True" VisibleIndex="1">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn Caption="Text" FieldName="Text" Name="col_Text" VisibleIndex="2">
</dx:GridViewDataTextColumn>
</Columns>
<SettingsEditing Mode="Batch" BatchEditSettings-ShowConfirmOnLosingChanges="false"></SettingsEditing>
</dx:ASPxGridView>
</form>
TestingWeb.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
DataTable DT = new DataTable();
DT.Columns.Add("Line");
DT.Columns.Add("Text");
DataRow r = DT.NewRow();
r["Line"] = 1;
r["Text"] = "Test";
DT.Rows.Add(r);
DataColumn[] key = new DataColumn[1];
key[0] = DT.Columns["Line"];
DT.PrimaryKey = key;
ASPxGridView1.DataSource = DT;
ASPxGridView1.DataBind();
Session["DataTable"] = DT;
}
protected void ASPxGridView1_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
{
DevExpress.Web.ASPxGridView.ASPxGridView gridView = (DevExpress.Web.ASPxGridView.ASPxGridView)sender;
DataTable DT = (DataTable)(Session["DataTable"]);
DataRow row = DT.Rows.Find(e.Keys[0]);
IDictionaryEnumerator enumerator = e.NewValues.GetEnumerator();
enumerator.Reset();
while (enumerator.MoveNext())
{
row[enumerator.Key.ToString()] = enumerator.Value;
}
gridView.CancelEdit();
e.Cancel = true;
Session["DataTable"] = DT;
}
I updated the version to 13.2.6 and the problem is solved.

Categories