System.Data.DataTable.LoadDataRow C# (CSharp) Method

LoadDataRow() public method

Finds and updates a specific row. If no matching row is found, a new row is created using the given values.
public LoadDataRow ( object values, LoadOption loadOption ) : DataRow
values object
loadOption LoadOption
return DataRow
        public DataRow LoadDataRow(object[] values, LoadOption loadOption)
        {
            long logScopeId = DataCommonEventSource.Log.EnterScope("<ds.DataTable.LoadDataRow|API> {0}, loadOption={1}", ObjectID, loadOption);
            try
            {
                Index indextoUse = null;
                if (_primaryKey != null)
                {
                    if (loadOption == LoadOption.Upsert)
                    {
                        // CurrentVersion, and Deleted
                        if (_loadIndexwithCurrentDeleted == null)
                        {
                            _loadIndexwithCurrentDeleted = _primaryKey.Key.GetSortIndex(DataViewRowState.CurrentRows | DataViewRowState.Deleted);
                            Debug.Assert(_loadIndexwithCurrentDeleted != null, "loadIndexwithCurrentDeleted should not be null");
                            if (_loadIndexwithCurrentDeleted != null)
                            {
                                _loadIndexwithCurrentDeleted.AddRef();
                            }
                        }
                        indextoUse = _loadIndexwithCurrentDeleted;
                    }
                    else
                    {
                        // CurrentVersion, and Deleted : OverwriteRow, PreserveCurrentValues
                        if (_loadIndexwithOriginalAdded == null)
                        {
                            _loadIndexwithOriginalAdded = _primaryKey.Key.GetSortIndex(DataViewRowState.OriginalRows | DataViewRowState.Added);
                            Debug.Assert(_loadIndexwithOriginalAdded != null, "loadIndexwithOriginalAdded should not be null");
                            if (_loadIndexwithOriginalAdded != null)
                            {
                                _loadIndexwithOriginalAdded.AddRef();
                            }
                        }
                        indextoUse = _loadIndexwithOriginalAdded;
                    }
                    // not expecting LiveIndexes to clear the index we use between calls to LoadDataRow
                    Debug.Assert(2 <= indextoUse.RefCount, "bad indextoUse.RefCount");
                }
                if (_inDataLoad && !AreIndexEventsSuspended)
                {
                    // we do not want to fire any listchanged in new Load/Fill
                    SuspendIndexEvents();// so suspend events here(not suspended == table already has some rows initially)
                }

                DataRow dataRow = LoadRow(values, loadOption, indextoUse);// if indextoUse == null, it means we dont have PK,
                                                                          // so LoadRow will take care of just adding the row to end

                return dataRow;
            }
            finally
            {
                DataCommonEventSource.Log.ExitScope(logScopeId);
            }
        }

Same methods

DataTable::LoadDataRow ( object values, bool fAcceptChanges ) : DataRow

Usage Example

        public static string aboutList()
        {
            StringBuilder strTxt = new StringBuilder();
            DataTable tbl = new DataTable();
            tbl.Columns.Add("Title", typeof(string));
            tbl.Columns.Add("Url", typeof(string));
            object[] aValues = { "公司简介", "About.aspx" };
            tbl.LoadDataRow(aValues, true);
            object[] aValues1 = { "招聘信息", "Jobs.aspx" };
            tbl.LoadDataRow(aValues1, true);
            object[] aValues2 = { "站点地图", "SiteMap.aspx" };
            tbl.LoadDataRow(aValues2, true);
            object[] aValues3 = { "联系我们", "ContactInfo.aspx" };
            tbl.LoadDataRow(aValues3, true);

            if (tbl.Rows.Count > 0)
            {
                strTxt.Append("<dl>");
                for (int j = 0; j < tbl.Rows.Count; j++)
                {
                    DataRow dr2 = tbl.Rows[j];
                    strTxt.Append("<dd style=\"background-color: rgb(239,239,239); height: 26px;\">");
                    strTxt.Append("<a class=\"channelClass01\" href=\""+ dr2["Url"].ToString() +"\" style=\"position: relative;top: 5px; left: 15px;\">" + dr2["Title"].ToString() + "</a>");
                    strTxt.Append("</dd>");
                }
                strTxt.Append("</dl>");
            }
            else
                strTxt.Append("暂无栏目!");
            return strTxt.ToString();
        }
All Usage Examples Of System.Data.DataTable::LoadDataRow
DataTable