Share via


C# DataTable Add Row As Header/Bold

Question

Tuesday, November 26, 2013 8:52 AM

How can I add a row in a C# datatable as a header or in bold font. My datatable is dynamically generated from the Database and here's my sample code of the row I want in bold text/header text.

DataTable.Rows.Add(row["Cell1"].ToString(), row["Cell2"].ToString(), row["Cell3"].ToString());

How can I add the above to my datatable as a headertext or in bold font.
Regards,
JohnSpax

All replies (5)

Tuesday, November 26, 2013 12:55 PM âś…Answered | 1 vote

hi,

test this code

******** cs code **********************************

            

 DataTable dt = new DataTable("ResultTable");
            dt.Columns.Add("Cell1", typeof(string));
            dt.Columns.Add("Cell2", typeof(string));
            dt.Columns.Add("Cell3", typeof(string));
            DataRow row = dt.NewRow();

            row[0] = "<strong>This is heading 1</strong>";
            row[1] = "<strong>This is heading 2</strong>";
            row[2] = "<strong>This is heading 3</strong>";
            dt.Rows.Add(row);
           dt.AcceptChanges();

            GridView1.DataSource = dt;
            GridView1.DataBind();

*******************************************************

*************** aspx code ******************************

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:TemplateField HeaderText="col1">
                    <ItemTemplate>
                        <asp:Label ID="lbl1" runat="server" Text='<%# Eval("Cell1")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                 <asp:TemplateField  HeaderText="col2">
                    <ItemTemplate>
                        <asp:Label ID="lbl2" runat="server" Text='<%#Eval("Cell2")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                 <asp:TemplateField  HeaderText="col3">
                    <ItemTemplate>
                        <asp:Label ID="lbl3" runat="server" Text='<%# Eval("Cell3")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

************************************************************

Regards

deepak


Tuesday, November 26, 2013 9:23 AM

Hi,  Welcomes to you MSDN forums.

I couldn't understand your question clearly. As per my understand you want to show the first row is header(like Column name) values, if I am right your approach is not correct. 

In DataTable you need to create a columns first, So that you can add the values to your datatable.

 DataTable dt = new DataTable("ResultTable");
 dt.Columns.Add(row["Cell1"].ToString());
 dt.Columns.Add(row["Cell2"].ToString());
 dt.Columns.Add(row["Cell3"].ToString());//do Your logic here and add the remaining result rows like the below.
 dt.Rows .Add(row["Cell1"].ToString(), row["Cell2"].ToString(), row["Cell3"].ToString());

I hope you may understood the above..

by

Elayaraja

http://www.ijcstjournal.org/

If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful"


Tuesday, November 26, 2013 9:36 AM

Hi Elayaraja,

Thanks for your quick response. Perhaps I did not phrase my question correctly. So here's my datatable:

                DataTable dt= new DataTable();

                dt.Columns.Add("Column1", typeof(string));
                dt.Columns.Add("Column2", typeof(string));
                dt.Columns.Add("Column3",typeof(string));

Here is how I bind:

dt.Rows.Add(row["Cell1"].ToString(), row["Cell2"].ToString(), row["Cell3"].ToString())

Now the Question is: There is another row that I need to add that should be in bold text(like a header).eg

<b>(dt.Rows.Add("Total Transactions:", "50", "Amount: $1500.00"))</b>

How can I achieve this?

Regards,

Johnspax


Tuesday, November 26, 2013 10:54 AM

In Data table Rows Or Columns Can not be marked as bold . As its a collection of data not decorating the data. The control in which you are showing the data can have row header or column header bold.

Satya R Biswal


Tuesday, November 26, 2013 12:31 PM

Hi,

As Sathya mentioned you cant bold or add styles in the data table. It is just a object which holds the data set. If you are using a Datagridview control use the following code to bold letter.

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
  var dataGridView = sender as DataGridView;
  if (dataGridView.Rows[e.RowIndex].Selected)
  {
    e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Bold);
    // edit: to change the background color:
    e.CellStyle.SelectionBackColor = Color.Coral;
  }
}

Thank You,

Sammani

http://sammanipalansuriya.blogspot.com/