thevan
thevan

Reputation: 10344

How to change the DataTable Column Name?

I have one DataTable which has four columns such as

 StudentID        CourseID          SubjectCode            Marks    
------------     ----------        -------------          --------
    1               100              MT400                  80
    2               100              MT400                  79
    3               100              MT400                  88

Here I am inserting this Datatable into the Sql server table by passing this datatable as an XML Table.

I just want to Change the DataTable Column Name "Marks" as "SubjectMarks" and pass this DataTable as an XML Table.

I know how to pass the DataTable as an XML Table. But I dont know, How to change the DataTable Column Name "Marks" as "SubjectMarks".

Upvotes: 144

Views: 321318

Answers (7)

Pankaj Agarwal
Pankaj Agarwal

Reputation: 11311

after generating XML you can just Replace your XML <Marks>... content here </Marks> tags with <SubjectMarks>... content here </SubjectMarks>tag. and pass updated XML to your DB.

Your XML Generate Like as below.

<NewDataSet>
      <StudentMarks> 
          <StudentID>1</StudentID>
          <CourseID>100</CourseID>
          <SubjectCode>MT400</SubjectCode>
          <Marks>80</Marks>
      </StudentMarks>
      <StudentMarks> 
          <StudentID>1</StudentID>
          <CourseID>100</CourseID>
          <SubjectCode>MT400</SubjectCode>
          <Marks>79</Marks>
      </StudentMarks>
      <StudentMarks> 
          <StudentID>1</StudentID>
          <CourseID>100</CourseID>
          <SubjectCode>MT400</SubjectCode>
          <Marks>88</Marks>
      </StudentMarks>
  </NewDataSet>

Here you can assign XML to string variable like as

string strXML = DataSet.GetXML();

strXML = strXML.Replace ("<Marks>","<SubjectMarks>");
strXML = strXML.Replace ("<Marks/>","<SubjectMarks/>");

and now pass strXML To your DB.

Upvotes: 1

tanteng
tanteng

Reputation: 668

try this

"columns": [
{data: "id", name: "aaa", sortable: false},
{data: "userid", name: "userid", sortable: false},
{data: "group_id", name: "group_id", sortable: false},
{data: "group_name", name: "group_name", sortable: false},
{data: "group_member", name: "group_member"},
{data: "group_fee", name: "group_fee"},
{data: "dynamic_type", name: "dynamic_type"},
{data: "dynamic_id", name: "dynamic_id"},
{data: "content", name: "content", sortable: false},
{data: "images", name: "images", sortable: false},
{data: "money", name: "money"},
{data: "is_audit", name: "is_audit", sortable: false},
{data: "audited_at", name: "audited_at", sortable: false}

]

enter image description here

Upvotes: 1

Subhash Saini
Subhash Saini

Reputation: 274

Use:

dt.Columns["Name"].ColumnName = "xyz";
dt.AcceptChanges();

or

dt.Columns[0].ColumnName = "xyz";
dt.AcceptChanges();

Upvotes: 8

Anurag Deokar
Anurag Deokar

Reputation: 849

Use this

dataTable.Columns["OldColumnName"].ColumnName = "NewColumnName";

Upvotes: 0

Hardik Shah
Hardik Shah

Reputation: 186

 dtTempColumn.Columns["EXCELCOLUMNS"].ColumnName = "COLUMN_NAME";                        
 dtTempColumn.AcceptChanges();

Upvotes: 6

Saurabh
Saurabh

Reputation: 5727

Rename the Column by doing the following:

dataTable.Columns["ColumnName"].ColumnName = "newColumnName";

Upvotes: 31

Moon
Moon

Reputation: 35265

Try this:

dataTable.Columns["Marks"].ColumnName = "SubjectMarks";

Upvotes: 307

Related Questions