Resolved I want to bind << gridview.DataSource = dt; gridview.DataBind(); >> using DataTable .

patrick

Well-known member
Joined
Dec 5, 2021
Messages
251
Programming Experience
1-3
I want to input data into a grid.


I want to bind << gridview.DataSource = dt; gridview.DataBind(); >> using DataTable .


Initialize the GridView======================
C#:
private void SetInitialRow()
{
DataTable dt = new DataTable();

for (int col = 0; col < 20; col++)
{
string sCol = Convert.ToString(col);
dt.Columns.Add(new DataColumn(sCol, typeof(string)));//for TextBox value
}

DataRow dr = null;
for (int i = 0; i < 22; i++)
{
dr = dt.NewRow();
dt.Rows.Add(dr);
}

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

Session["DT"] = dt;

int nrow = 0;
int ncolNo = 0;
int nTot = 0;
foreach (GridDataItem item in gv.Items)
{
nrow++;
foreach (GridColumn col in gv.Columns)
{
ncolNo++;
string str = "RadTextBox" + Convert.ToString(ncolNo);
RadTextBox Box = item.FindControl(str) as RadTextBox;
if (nrow % 2 == 1)
{
nTot++;
Box.Text = Convert.ToString(nTot);
Box.Enabled = false;
Box.BackColor = System.Drawing.Color.Aqua;
}
}
ncolNo = 0;
}
}

******** the function in Probelm===>
*********Input to the GridView======================

C#:
 protected void ibtnInquiry_Click(object sender, ImageClickEventArgs e)
{
 DataTable dtRecord = (DataTable)Session["DT"];

            int nrow = 0;
            int ncolNo = 0;
            foreach (DataRow dr in dtRecord.Rows)
            {
                string str = Convert.ToString(ncolNo);

                for(int i = 0; i < 20; i++)
                {
                    if (nrow % 2 == 0)
                    {
                        string sNo = Convert.ToString(i);
                        dr[sNo] = arrECOSVal[ncolNo].ToString();
                    }
                }

                nrow++;
            }

            gv.DataSource = dtRecord;
            gv.DataBind();
}
GridtxtBox.png
 
Last edited by a moderator:
Like we've told you in the past, please put code in code tags. It helps preserve the formatting of your code and makes reading the code easier. At this point, even though it looks like some the indents are lost before I got a chance to put the code in code tags for you. I do have to thank you for stopping posting code screenshots, and actually using text now. :) Now just need to move up one more step to posting code text in code tags. You can use the toolbar button that looks like </> for multiline code.
 
What problem are you running into?
 
What exactly are you trying to do? Perhaps as data grid is not the right UI.
 
I can see that is what you are trying to do currently. What I'm asking is what is the purpose of this. What do those numbers you prefill on alternate rows represent? What do those blank rows represent? Right now I have a feeling that is going to be another XY problem like your other threads.

This is why I am always hesitant about replying to your threads after there first few times. Trying to get information out of you is like pulling teeth. More often than not, you would just repeat the same question you had in your opening post instead of explaining further, or you just completely ignore the question. We want to help you, but you have been not making it easy.
 
Looks like I got the example source I wanted.

C#:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    #region constants
    const string NAME = "NAME";
    const string NAME2 = "NAME2";
    const string ID = "ID";
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            loadDynamicGrid();
    }
    
    private void loadDynamicGrid()
    {
        #region Code for preparing the DataTable

        //Create an instance of DataTable
        DataTable dt = new DataTable();

        //Create an ID column for adding to the Datatable.
        DataColumn dcol = new DataColumn(ID, typeof(System.Int32));
        dcol.AutoIncrement = true;
        dt.Columns.Add(dcol);

        //Create an editable Name column for adding to the Datatable.
        dcol = new DataColumn(NAME, typeof(System.String));

        //This is one of the good ability, where I can define more attributes for the column.
        dcol.ExtendedProperties["Editable"] = true;                      //Adding an extra custom property for the column.
        dt.Columns.Add(dcol);
        
        //Create an non-editable column for adding to the Datatable.
        dcol = new DataColumn(NAME2, typeof(System.String));
        dcol.ExtendedProperties["Editable"] = false;                      //Adding an extra custom property for the column.
        dt.Columns.Add(dcol);

        //Create one more column for displaying the checkbox.
        dcol = new DataColumn("CHECKBOX", typeof(System.String));
        dcol.ExtendedProperties["CheckBox"] = true;                      //Adding an extra custom property for the column.
        dt.Columns.Add(dcol);
        
        //Now add data for dynamic columns.
        //As the first column is auto-increment, we do not have to add any thing.
        //Let's add some data to the second column.
        for (int nIndex = 0; nIndex < 10; nIndex++)
        {
            //Create a new row
            DataRow drow = dt.NewRow();

            //Initialize the row data.
            drow[NAME] = "Name:" + (nIndex + 1);
            drow[NAME2] = NAME2 + ":" + (nIndex + 1);
            
            //Add the row to the datatable.
            dt.Rows.Add(drow);
        }
        #endregion

        //Iterate through the columns of the datatable to set the data bound field dynamically.
        foreach (DataColumn col in dt.Columns)
        {
            //Declare the bound field and allocate memory for the bound field.
            CustomBoundField bfield = new CustomBoundField();

            //Initalize the DataField value.
            bfield.DataField = col.ColumnName;

            //Initialize the HeaderText field value.
            bfield.HeaderText = col.ColumnName;

            //Check if any property has been defined for the editing.
            if (col.ExtendedProperties["Editable"] != null)
            {
                bfield.Editable = Convert.ToBoolean(col.ExtendedProperties["Editable"]);
            }

            //Check if any property has been defined for displaying the checkboxes.
            if (col.ExtendedProperties["CheckBox"] != null)
            {
                bfield.ShowCheckBox = Convert.ToBoolean(col.ExtendedProperties["CheckBox"]);
            }

            //Add the newly created bound field to the GridView.
            GrdDynamic.Columns.Add(bfield);
        }

        //Initialize the DataSource
        GrdDynamic.DataSource = dt;

        //Bind the datatable with the GridView.
        GrdDynamic.DataBind();
    }
    protected void cmdSave_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow orow in GrdDynamic.Rows)
        {
            foreach (TableCell cell in orow.Cells)
            {
                TextBox txtBox = (TextBox)cell.Controls[0];
                Response.Write(txtBox.Text);
            }
            Response.Write("<BR>");
        }
    }
}
 
And where is CustomBoundField found in ASP.NET? Sounds like you are using the code adapted from here:

Notice the difference between the title of that CodeProject article and what you are asking for in this thread. You were asking about binding to data to data grid, but the article talks about custom controls for data bound to a grid view.
 
I solved the problem similar to this example source.And where is CustomBoundField found in ASP.NET? Sounds like you are using the code adapted from here:

Notice the difference between the title of that CodeProject article and what you are asking for in this thread. You were asking about binding to data to data grid, but the article talks about custom controls for data bound to a grid view.
I referenced this example source and made it similar to the example source.
Thanks for your help in solving the problem.
 
Back
Top Bottom