Catching the content of DataGrid at runtime
-
Hello, here is my source for catching the content of DataGrid: <%# DataBinder.Eval(Container.DataItem, "Thread_Content") %> and here is my code-behind:
private void Page_Load(object sender, System.EventArgs e) { ThreadsList.DataSource = CreateDataSource(); ThreadsList.DataBind(); } // The ItemCreated event (of the DataGrid control) occur when an Item is created private void ThreadsList_ItemCreated(object sender, DataGridItemEventArgs e) { if (e.Item.ItemIndex != -1) { // the Thread_Content column has the index is 2 ((DataRowView)e.Item.DataItem).Row["2"] = ((DataRowView)e.Item.DataItem).Row[2].ToString().Replace("\n", " "); } }
above is my source to catch the content of the column Thread_Content at runtime to replace any "\n" to "
" before they bind to the control so that it will make my thread content break line. And here is my problem: at the first time when loading the page (IsPostBack return false), the source work fine, it does not have any error. But when IsPostBack return true, for instance I click the delete button in DataGrid to delete a row, i get the error: "Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.", the error occur at the line: ((DataRowView)e.Item.DataItem).Row["2"] = ((DataRowView)e.Item.DataItem).Row[2].ToString().Replace("\n", "
"); That is!!! I can not understand the reason, anyone can help me, plz show me a way. Thanks a lot !!!:):) -
Hello, here is my source for catching the content of DataGrid: <%# DataBinder.Eval(Container.DataItem, "Thread_Content") %> and here is my code-behind:
private void Page_Load(object sender, System.EventArgs e) { ThreadsList.DataSource = CreateDataSource(); ThreadsList.DataBind(); } // The ItemCreated event (of the DataGrid control) occur when an Item is created private void ThreadsList_ItemCreated(object sender, DataGridItemEventArgs e) { if (e.Item.ItemIndex != -1) { // the Thread_Content column has the index is 2 ((DataRowView)e.Item.DataItem).Row["2"] = ((DataRowView)e.Item.DataItem).Row[2].ToString().Replace("\n", " "); } }
above is my source to catch the content of the column Thread_Content at runtime to replace any "\n" to "
" before they bind to the control so that it will make my thread content break line. And here is my problem: at the first time when loading the page (IsPostBack return false), the source work fine, it does not have any error. But when IsPostBack return true, for instance I click the delete button in DataGrid to delete a row, i get the error: "Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.", the error occur at the line: ((DataRowView)e.Item.DataItem).Row["2"] = ((DataRowView)e.Item.DataItem).Row[2].ToString().Replace("\n", "
"); That is!!! I can not understand the reason, anyone can help me, plz show me a way. Thanks a lot !!!:):)The DataItem hasn't been built yet in the ItemCreated event, you need to use the ItemDataBound event instead. Hope that helps, Marcie http://www.codeproject.com
-
The DataItem hasn't been built yet in the ItemCreated event, you need to use the ItemDataBound event instead. Hope that helps, Marcie http://www.codeproject.com
-
The DataItem hasn't been built yet in the ItemCreated event, you need to use the ItemDataBound event instead. Hope that helps, Marcie http://www.codeproject.com
thanks for yr replying, before posting my problem i tried to use the ItemDataBound event so that i can not catch my content at runtime.
-
Hello, here is my source for catching the content of DataGrid: <%# DataBinder.Eval(Container.DataItem, "Thread_Content") %> and here is my code-behind:
private void Page_Load(object sender, System.EventArgs e) { ThreadsList.DataSource = CreateDataSource(); ThreadsList.DataBind(); } // The ItemCreated event (of the DataGrid control) occur when an Item is created private void ThreadsList_ItemCreated(object sender, DataGridItemEventArgs e) { if (e.Item.ItemIndex != -1) { // the Thread_Content column has the index is 2 ((DataRowView)e.Item.DataItem).Row["2"] = ((DataRowView)e.Item.DataItem).Row[2].ToString().Replace("\n", " "); } }
above is my source to catch the content of the column Thread_Content at runtime to replace any "\n" to "
" before they bind to the control so that it will make my thread content break line. And here is my problem: at the first time when loading the page (IsPostBack return false), the source work fine, it does not have any error. But when IsPostBack return true, for instance I click the delete button in DataGrid to delete a row, i get the error: "Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.", the error occur at the line: ((DataRowView)e.Item.DataItem).Row["2"] = ((DataRowView)e.Item.DataItem).Row[2].ToString().Replace("\n", "
"); That is!!! I can not understand the reason, anyone can help me, plz show me a way. Thanks a lot !!!:):)Have you considered doing the string replacement at the database level? Unless you have a need for the "\n" version elsewere in your application, filtering in the query you use to populate the dataset would make your code cleaner and perform [a tiny bit] better. Hope that helps. :) --Jesse