i have created a datatable like parenttable and childtable. i have given datarelation to both.but the output is showing unnecessary columns because they are using primary keys and foreign keys for making data relation.how to hide them and also how to remove the relation name in the output?
Loading

Naimish MakwanaRank #324 · 7.7K reputationPosted Dec 27, 2023, 1:04 AM EST
Hide Primary and Foreign Key Columns:
Visibleproperty of the unwanted columns tofalse. For example:dataGridView1.Columns["PrimaryKeyColumnName"].Visible = false; dataGridView1.Columns["ForeignKeyColumnName"].Visible = false;SetColumnsmethod to hide the columns you don't want to display. Here's an example:parentTable.Columns["PrimaryKeyColumnName"].ColumnMapping = MappingType.Hidden; childTable.Columns["ForeignKeyColumnName"].ColumnMapping = MappingType.Hidden;Remove Relation Name:
RelationNameproperty. You can set it to an empty string or null to remove the name from the output.DataRelation dataRelation = new DataRelation("YourRelationName", parentTable.Columns["PrimaryKeyColumnName"], childTable.Columns["ForeignKeyColumnName"]); dataSet.Relations.Add(dataRelation); // Remove the relation name dataRelation.RelationName = string.Empty; // or nullIf you've already added the relation to a DataSet, you can modify the
RelationNameproperty directly:dataSet.Relations["YourRelationName"].RelationName = string.Empty; // or nullRemember to replace "PrimaryKeyColumnName", "ForeignKeyColumnName", and "YourRelationName" with the actual names used in your implementation.
Thanks
Prasad RaveendranRank #320 · 7.8K reputationPosted Dec 28, 2023, 11:23 PM EST
Let's say you have two DataTables
parentTableandchildTablewith a DataRelation between them:DataRelation relation = new DataRelation("RelationName", parentTable.Columns["ParentPrimaryKeyColumn"], childTable.Columns["ChildForeignKeyColumn"]); // Assuming you have added the DataRelation to your DataSet dataSet.Relations.Add(relation);To display data without the keys and only specific columns:
var result = from parentRow in parentTable.AsEnumerable() join childRow in childTable.AsEnumerable() on parentRow["PrimaryKeyColumn"] equals childRow["ForeignKeyColumn"] select new { // Select only the necessary columns ParentColumn1 = parentRow["ParentColumn1"], ParentColumn2 = parentRow["ParentColumn2"], ChildColumn1 = childRow["ChildColumn1"], ChildColumn2 = childRow["ChildColumn2"] // Add more columns as needed }; // Bind 'result' to your UI or process it further // This 'result' will contain only the selected columns without showing primary/foreign keysThis LINQ query fetches the necessary columns from both tables based on the DataRelation and creates a result set that excludes the primary and foreign key columns.
Jayraj ChhayaRank #304 · 8.2K reputationPosted Dec 27, 2023, 2:42 AM EST
Hi Naveen,
Hiding Unnecessary Columns and Removing Relation Name in DataRelation Outputusing System; using System.Data; class Program { static void Main(string[] args) { // Create parent table DataTable parentTable = new DataTable("ParentTable"); parentTable.Columns.Add("ParentID", typeof(int)); parentTable.Columns.Add("ParentName", typeof(string)); // Create child table DataTable childTable = new DataTable("ChildTable"); childTable.Columns.Add("ChildID", typeof(int)); childTable.Columns.Add("ChildName", typeof(string)); childTable.Columns.Add("ParentID", typeof(int)); // Create data relation DataRelation relation = new DataRelation("ParentChildRelation", parentTable.Columns["ParentID"], childTable.Columns["ParentID"]); parentTable.Constraints.Add(relation); // Hide unnecessary columns childTable.Columns["ParentID"].ColumnMapping = MappingType.Hidden; // Remove relation name in output relation.RelationName = string.Empty; // Print the data relation output Console.WriteLine("Data Relation Output:"); Console.WriteLine(relation.ToString()); } }This code demonstrates how to hide unnecessary columns and remove the relation name in the output of a DataRelation in C#. The code creates a parent table and a child table, and establishes a data relation between them. By setting the
ColumnMappingproperty of the unnecessary column toMappingType.Hidden, the column will not be displayed in the output. Additionally, setting theRelationNameproperty of the DataRelation to an empty string will remove the relation name from the output.Anandu G NathRank #681 · 3.5K reputationPosted Dec 28, 2023, 10:48 PM EST
Check Below code
// Assuming dtParent and dtChild are the parent and child DataTables respectively DataRelation dataRelation = new DataRelation("RelationName", dtParent.Columns["ParentColumn"], dtChild.Columns["ChildColumn"]); dtParent.ParentRelations.Add(dataRelation); // Bind the DataTables to a DataGridView (WinForms) or GridView (ASP.NET) dataGridView1.DataSource = dtParent; dataGridView1.Columns["ParentColumn"].Visible = false; // Hide the primary key column in the parent table dataGridView1.Columns["ChildColumn"].Visible = false; // Hide the foreign key column in the child table// Create a new DataTable without primary and foreign key columns DataTable displayTable = new DataTable(); foreach (DataColumn column in dtParent.Columns) { if (column.ColumnName != "ParentColumn") // Exclude the primary key column { displayTable.Columns.Add(column.ColumnName); } } // Copy data from the original table to the display table foreach (DataRow parentRow in dtParent.Rows) { DataRow newRow = displayTable.Rows.Add(); foreach (DataColumn column in dtParent.Columns) { if (column.ColumnName != "ParentColumn") // Exclude the primary key column { newRow[column.ColumnName] = parentRow[column.ColumnName]; } } } // Bind the displayTable to the UI control (e.g., DataGridView or GridView) dataGridView1.DataSource = displayTable;Rameez Hasan153 reputationPosted Dec 31, 2023, 2:33 AM EST
If you want to hide the primary and foreign key columns in the output when using
1. Hiding Primary and Foreign Key Columns:DataRelationin ADO.NET DataTables, you can follow these steps:Assuming you have created a
DataRelationbetweenparentTableandchildTable, and you want to hide the primary and foreign key columns:// Assuming you have created parentTable and childTable // Create DataRelation DataRelation dataRelation = new DataRelation("YourRelationName", parentTable.Columns["ParentID"], childTable.Columns["ParentID"]); // Add DataRelation to DataSet DataSet dataSet = new DataSet(); dataSet.Tables.Add(parentTable); dataSet.Tables.Add(childTable); dataSet.Relations.Add(dataRelation); // Hide primary key in childTable childTable.Columns["ParentID"].ColumnMapping = MappingType.Hidden;By setting
2. Removing Relation Name in Output:ColumnMappingtoMappingType.Hidden, you can hide the primary key column in the child table.If you want to remove the relation name in the output, you can do the following:
// Assuming you have created parentTable and childTable // Create DataRelation without specifying a name DataRelation dataRelation = new DataRelation(string.Empty, parentTable.Columns["ParentID"], childTable.Columns["ParentID"]); // Add DataRelation to DataSet DataSet dataSet = new DataSet(); dataSet.Tables.Add(parentTable); dataSet.Tables.Add(childTable); dataSet.Relations.Add(dataRelation);By providing an empty string or not specifying a name when creating the
DataRelation, you can avoid having a name for the relation.Remember to adapt the column names (
ParentIDin this example) and relation names to match your actual data structure and requirements. Adjust the code according to your specific implementation.