4
Hide Primary and Foreign Key Columns:
- If you're displaying the DataTable in a UI control (like a DataGridView), you can set the
Visible
property of the unwanted columns to false
. For example:
dataGridView1.Columns["PrimaryKeyColumnName"].Visible = false;
dataGridView1.Columns["ForeignKeyColumnName"].Visible = false;
- If you're working with the DataTable directly, you can use the
SetColumns
method 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:
- The DataRelation class has a
RelationName
property. 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 null
If you've already added the relation to a DataSet, you can modify the RelationName
property directly:
dataSet.Relations["YourRelationName"].RelationName = string.Empty; // or null
Remember to replace "PrimaryKeyColumnName", "ForeignKeyColumnName", and "YourRelationName" with the actual names used in your implementation.
Thanks

3
Let's say you have two DataTables parentTable
and childTable
with 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 keys
This 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.

3
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;

2
If you want to hide the primary and foreign key columns in the output when using DataRelation
in ADO.NET DataTables, you can follow these steps:
1. Hiding Primary and Foreign Key Columns:
Assuming you have created a DataRelation
between parentTable
and childTable
, 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 ColumnMapping
to MappingType.Hidden
, you can hide the primary key column in the child table.
2. Removing Relation Name in Output:
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 (ParentID
in this example) and relation names to match your actual data structure and requirements. Adjust the code according to your specific implementation.

2
Hi Naveen,
Hiding Unnecessary Columns and Removing Relation Name in DataRelation Output
using 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 ColumnMapping
property of the unnecessary column to MappingType.Hidden
, the column will not be displayed in the output. Additionally, setting the RelationName
property of the DataRelation to an empty string will remove the relation name from the output.
