locked
My Gridview First Column Data To Show In my code RRS feed

  • Question

  • User2033107836 posted

    Hello

    This is my code

    GridViewRow ytopheaderrow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
    
            TableCell ycellheder = new TableCell();
            ycellheder.Text = "How To Pass My Gridview First Column Data"
            ycellheder.HorizontalAlign = HorizontalAlign.Center;
            ycellheder.ColumnSpan = 5;
            ycellheder.CssClass = "aSubTotalRowStyle";
            ytopheaderrow.Cells.Add(ycellheder);
            GridView2.Controls[0].Controls.AddAt(0, ytopheaderrow);

    How to do so..?

    Thanking You

    Wednesday, May 30, 2018 1:22 AM

Answers

  • User283571144 posted

    Hi asp.ambur,

    Thank You So Much For Your Code..

    Yes Exactly The Same I Want 

    But Can i View Data Like this

    Like Each Row For Example below

    ID21d8c2a0-ec90-4313-a96e-f06145523a4c

    8d2e803c-b824-4902-bdaf-3c3a5fe3b057

    Instead of 

    ID21d8c2a0-ec90-4313-a96e-f06145523a4c8d2e803c-b824-4902-bdaf-3c3a5fe3b057

    Thanking You

    According to your description, I suggest you could add the "<br/>" at the end of each line.

    More details, you could refer to below code sample:

            protected void Button1_Click(object sender, EventArgs e)
            {
                GridViewRow ytopheaderrow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
                TableCell ycellheder = new TableCell();
                StringBuilder b1 = new StringBuilder();
    
                  b1.AppendLine(((TableCell)GridView1.Controls[0].Controls[0].Controls[0]).Text + "</br>");
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                      b1.AppendLine(((Label)GridView1.Rows[i].FindControl("Label1")).Text + "</br>");
                }
    
                ycellheder.Text = b1.ToString();
                ycellheder.HorizontalAlign = HorizontalAlign.Center;
                ycellheder.ColumnSpan = 5;
                ycellheder.CssClass = "aSubTotalRowStyle";
                ytopheaderrow.Cells.Add(ycellheder);
                GridView1.Controls[0].Controls.AddAt(0, ytopheaderrow);
            }

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 6, 2018 6:55 AM

All replies

  • User-369506445 posted

    hi

    please try below code :

    protected void Page_Load(object sender, EventArgs e)
            {
    
                var gv = GridView2;
                var t = new Table();
                gv.Controls.Add(t);
    
                GridViewRow ytopheaderrow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
    
                TableCell ycellheder = new TableCell();
                ycellheder.Text = "How To Pass My Gridview First Column Data";
                ycellheder.HorizontalAlign = HorizontalAlign.Center;
                ycellheder.ColumnSpan = 5;
                ycellheder.CssClass = "aSubTotalRowStyle";
                ytopheaderrow.Cells.Add(ycellheder);
                GridView2.Controls[0].Controls.AddAt(0, ytopheaderrow);
    
    
    
            }

    Wednesday, May 30, 2018 9:03 AM
  • User2033107836 posted

    Hello

    Can you give me asp.net 2.0 C# code..

    here i want to pass

    ycellheder.Text = "How To Pass My Gridview First Column Data here";

    Wednesday, May 30, 2018 11:09 AM
  • User-369506445 posted

    it's a different thing, you can create a Model and pass your model to view

    for example create a Model below like :

     public class MyModel
        {
            public string Name { get; set; }
        }

    and in controller

    public IActionResult Index()
            {
                List<MyModel> list = new List<MyModel>();
                list.Add(new MyModel() { Name = "Row1" });
                list.Add(new MyModel() { Name = "Row2" });
    
                return View(list);
            }

    and in View

    @model IEnumerable<WebApplication1.Model.MyModel>
    
    @{
        ViewData["Title"] = "Home Page";
    } 
    <table>
        <tr>
            <th>Col1</th>
        </tr>
       @foreach (var item in Model)
       {
        <tr>
            <td>@item.Name</td>
        </tr>
    
       }
    </table>

    Wednesday, May 30, 2018 12:07 PM
  • User283571144 posted

    Hi asp.ambur,

    According to your descriptin and codes, I couldn't understand your issue clearly.

    Do you mean you want to dynamically generate the first row or head for the girdview in code-behind?

    Could you please post the details error message and the details requiremnets about your issue?

    If you could post more details inforamtion, it will be more easily for us to understand your issue and find the solution.

    Best Regards,

    Brando

    Thursday, May 31, 2018 3:14 AM
  • User2033107836 posted

    Hello

    Below for reference i used to show textbox1 data in ycellheder.Text

    GridViewRow ytopheaderrow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);

    string Textbox1data = Textbox1.text;

    TableCell ycellheder = new TableCell();
    ycellheder.Text = Textbox1data;
    ycellheder.HorizontalAlign = HorizontalAlign.Center;
    ycellheder.ColumnSpan = 5;
    ycellheder.CssClass = "aSubTotalRowStyle";
    ytopheaderrow.Cells.Add(ycellheder);
    GridView2.Controls[0].Controls.AddAt(0, ytopheaderrow);


    like this above i want to pass my gridview1 first column data in ycellherder.text =


    how to pass my gridview first column data..

    thanking You

    Thursday, May 31, 2018 6:27 AM
  • User2033107836 posted
    Hello

    Any Help On my thread...

    Thanking You
    Tuesday, June 5, 2018 11:13 PM
  • User283571144 posted

    Hi asp.ambur,

    like this above i want to pass my gridview1 first column data in ycellherder.text =


    how to pass my gridview first column data..

    According to your description, I still couldn't understand your requirements claerly.

    Do you mean you want to get all the first column data and pass to the ycellheder.text?

    If this is your requirement, I suggest you could firstly get the girdview header's first column text then loop the girdview row to get the data.

    More details, you could refer to below codes:

            protected void Button1_Click(object sender, EventArgs e)
            {
                GridViewRow ytopheaderrow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
                TableCell ycellheder = new TableCell();
                string b1 = (((TableCell)GridView1.Controls[0].Controls[0].Controls[0]).Text);
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                       b1 += (((Label)GridView1.Rows[i].FindControl("Label1")).Text);
                }
    
                ycellheder.Text = b1.ToString();
                ycellheder.HorizontalAlign = HorizontalAlign.Center;
                ycellheder.ColumnSpan = 5;
                ycellheder.CssClass = "aSubTotalRowStyle";
                ytopheaderrow.Cells.Add(ycellheder);
                GridView1.Controls[0].Controls.AddAt(0, ytopheaderrow);
            }

    Result:

    Best Regards,

    Brando

    Wednesday, June 6, 2018 6:29 AM
  • User2033107836 posted

    Hello Brando ZWZ

    Thank You So Much For Your Code..

    Yes Exactly The Same I Want 

    But Can i View Data Like this

    Like Each Row For Example below

    ID21d8c2a0-ec90-4313-a96e-f06145523a4c

    8d2e803c-b824-4902-bdaf-3c3a5fe3b057

    Instead of 

    ID21d8c2a0-ec90-4313-a96e-f06145523a4c8d2e803c-b824-4902-bdaf-3c3a5fe3b057

    Thanking You

    Wednesday, June 6, 2018 6:42 AM
  • User283571144 posted

    Hi asp.ambur,

    Thank You So Much For Your Code..

    Yes Exactly The Same I Want 

    But Can i View Data Like this

    Like Each Row For Example below

    ID21d8c2a0-ec90-4313-a96e-f06145523a4c

    8d2e803c-b824-4902-bdaf-3c3a5fe3b057

    Instead of 

    ID21d8c2a0-ec90-4313-a96e-f06145523a4c8d2e803c-b824-4902-bdaf-3c3a5fe3b057

    Thanking You

    According to your description, I suggest you could add the "<br/>" at the end of each line.

    More details, you could refer to below code sample:

            protected void Button1_Click(object sender, EventArgs e)
            {
                GridViewRow ytopheaderrow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
                TableCell ycellheder = new TableCell();
                StringBuilder b1 = new StringBuilder();
    
                  b1.AppendLine(((TableCell)GridView1.Controls[0].Controls[0].Controls[0]).Text + "</br>");
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                      b1.AppendLine(((Label)GridView1.Rows[i].FindControl("Label1")).Text + "</br>");
                }
    
                ycellheder.Text = b1.ToString();
                ycellheder.HorizontalAlign = HorizontalAlign.Center;
                ycellheder.ColumnSpan = 5;
                ycellheder.CssClass = "aSubTotalRowStyle";
                ytopheaderrow.Cells.Add(ycellheder);
                GridView1.Controls[0].Controls.AddAt(0, ytopheaderrow);
            }

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 6, 2018 6:55 AM