Reputation: 23
I have done this thing and it displays an error as Index was out of range. Must be negative and non zero value.
In the following code,
Dim index_flag as Integer = 0
GridView1.Rows(index_flag).Cells(1).Text = ds.Tables(0).Rows(index_flag).Item("curr_datetime").ToString
GridView1.Rows(index_flag).Cells(2).Text = ds.Tables(0).Rows(index_flag).Item("site_id").ToString
GridView1.Rows(index_flag).Cells(2).Text = ds.Tables(0).Rows(index_flag).Item("site_name").ToString
GridView1.Rows(index_flag).Cells(3).Text = ds.Tables(0).Rows(index_flag).Item("dc_volt").ToString
Upvotes: 2
Views: 35188
Reputation: 49
Please try this, how it will help you.
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dt = new DataTable();
MakeDataTable();
}
else
{
dt = (DataTable)ViewState["DataTable"];
}
ViewState["DataTable"] = dt;
}
private void MakeDataTable()
{
dt.Columns.Add("Name");
dt.Columns.Add("Number");
}
protected void Button2_Click(object sender, EventArgs e)
{
AddToDataTable();
BindGrid();
}
private void AddToDataTable()
{
DataRow dr = dt.NewRow();
dr["Name"] = txtName.Text;
dr["Number"] = txtNumber.Text;
dt.Rows.Add(dr);
}
private void BindGrid()
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
Upvotes: 3
Reputation: 3169
By Manually add values from textbox to gridview the following method is easy. try this
code
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataColumn dc = new DataColumn();
if (dt.Columns.Count == 0)
{
dt.Columns.Add("Title1", typeof(string));
dt.Columns.Add("Title2", typeof(string));
}
DataRow NewRow = dt.NewRow();
NewRow[0] = TextBox1.Text;
NewRow[1] = TextBox2.Text;
dt.Rows.Add(NewRow);
GridView1.DataSource = dt;
GridView1.DataBind();
}
Upvotes: 1
Reputation: 2146
You are missing the GridView1.Rows.Add()
Dim index_flag as Integer = 0
GridView.Rows.Add()
GridView1.Rows(index_flag).Cells(1).Text = ds.Tables(0).Rows(index_flag).Item("curr_datetime").ToString
GridView1.Rows(index_flag).Cells(2).Text = ds.Tables(0).Rows(index_flag).Item("site_id").ToString
GridView1.Rows(index_flag).Cells(2).Text = ds.Tables(0).Rows(index_flag).Item("site_name").ToString
GridView1.Rows(index_flag).Cells(3).Text = ds.Tables(0).Rows(index_flag).Item("dc_volt").ToString
UPDATE
For Asp GridView check this link I have reviewed your question, you can check on this link http://www.codeproject.com/Articles/467788/Dynamically-adding-and-deleting-rows-from-ASP-NET
Upvotes: 0
Reputation: 1649
I think you should use datatable. Prepare your datatable as per your need and bind this datatable to your grid view here is the example of C# but you can do same thing with syntax of VB.NET
DataTable dt = new DataTable();
dt.Columns.Add("sr_no");
dt.Columns.Add("item_name");
dt.Columns.Add("item_id");
dt.Columns.Add("qty");
dt.Columns.Add("rate");
dt.Columns.Add("total");
var dr = dt.NewRow();
dr["sr_no"] = txtSr.Text;
dr["item_name"] = ddlItem.SelectedItem.Text;
dr["item_id"] = ddlItem.SelectedValue;
dr["qty"] = txtQty.Text;
dr["rate"] = txtRate.Text;
dr["total"] = int.Parse(txtQty.Text) * int.Parse(txtRate.Text);
dt.Rows.Add(dr);
Upvotes: 8
Reputation: 334
Why are you trying to add each and every row from the datatable to the GridView? As much as possible please use GridView.DataSource = DataTable and GridView. DataBind(). Which will do the job of adding the data from the data table to the grid view.
Upvotes: 0