To bind grid view with textbox - c#

In the below code i have a session value in which i have to pass to the grid and bind the values.The grid consists of textboxes if the session values is 2 there should be two row of textbox.I tried it throws index was out of range.Pls help me to over this issue.
int GoodsReceivedNoteID = (int)Session["GoodsReceivedNoteID"];
for (int iRow = 0; iRow < GoodsReceivedNoteID; iRow++)
{
TextBox txtFreightCharges = (TextBox)gvGRN.Rows[iRow].Cells[6].FindControl("txtFreightCharges");
TextBox txtLoadingCost = (TextBox)gvGRN.Rows[iRow].Cells[6].FindControl("txtLoadingCost");
TextBox txtUnloadingCost = (TextBox)gvGRN.Rows[iRow].Cells[6].FindControl("txtUnloadingCost");
TextBox txtInsuranseCost = (TextBox)gvGRN.Rows[iRow].Cells[6].FindControl("txtInsuranseCost");
TextBox txtOtherExpenses = (TextBox)gvGRN.Rows[iRow].Cells[6].FindControl("txtOtherExpenses");
}

Don't use Rows[index].Cells[6].FindControl, the cell is not the NamingContainer of a control in a GridViewRow's TemplateField but the row itself. I also don't understand the relation between your session value and the number of rows in the grid. This is simpler and more readable:
foreach(GridViewRow row in gvGRN.Rows)
{
TextBox txtFreightCharges = (TextBox)row.FindControl("txtFreightCharges");
TextBox txtLoadingCost = (TextBox)row.FindControl("txtLoadingCost");
TextBox txtUnloadingCost = (TextBox)row.FindControl("txtUnloadingCost");
TextBox txtInsuranseCost = (TextBox)row.FindControl("txtInsuranseCost");
TextBox txtOtherExpenses = (TextBox)row.FindControl("txtOtherExpenses");
}
If you only want to take GoodsReceivedNoteID-rows (which sounds wrong since an ID is not a counter):
for(int i = 0; i < GoodsReceivedNoteID; i++))
{
GridViewRow row = gvGRN.Rows[i];
TextBox txtFreightCharges = (TextBox)row.FindControl("txtFreightCharges");
// ...
}

Related

Datagrid view text boxes losing value

I have a Data grid view in which I have changed all the cells to text boxes to allow for a bulk update. The problem is that the users new input values are not registering and instead the cells are holding the old values. I taught it might be to do with a post back issue but cant imagine it is as there is no loading of old data in the page load. I also considered that the text boxes might not be registering properly because they are added without an ID but not to sure on that.
Changed cells to textboxes here.
foreach (GridViewRow row in GridView1.Rows)
{
for (int i = 4; i < row.Cells.Count; i++)
{
TextBox test = new TextBox();
test.Text = row.Cells[i].Text.ToString();
row.Cells[i].Controls.Add(test);
}
}
trying to pull new values and them to list newValues here
foreach(GridViewRow row in GridView1.Rows) {
if (row.RowType == DataControlRowType.DataRow) {
bool isChecked = row.Cells[0].Controls.OfType < CheckBox > ().FirstOrDefault().Checked;
if (isChecked) {
string table = DropDownList1.Text.ToString();
List < string > selectedValues = CheckBoxList1.Items.Cast < ListItem > ()
.Where(li = > li.Selected)
.Select(li = > li.Value)
.ToList();
List < string > newValues = new List < string > ();
int userid = Convert.ToInt32(row.Cells[1].Text.ToString());
//GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lblID = (Label) row.FindControl("WDB_Id");
//TextBox txtname=(TextBox)gr.cell[].control[];
for (int i = 4; i < selectedValues.Count + 4; i++) {
newValues.Add(row.Cells[i].Text.ToString());
}
}
}
}
Thanks

How to select one column from datagrid and add data for the selected column from field text?

I want to select column from data grid view.
Then, I want to add data in cells number 5 for the selected column.
Below code is not worked.
for (int i = 0; i < dgvSRP.Rows.Count; ++i)
{
dgvSRP.Rows.Add();
DataGridViewRow row = this.dgvSRP.Rows[i];
row.Cells[5].Value =txtJumlahPEsanan.Text;
}
foreach(var dgvRow in dgvSRP.Rows)
{
dgvRow.Cells[4].Value = txtJumlahPEsanan.Text; // The 5th column is index 4. Indexes always start at 0.
}
Here is my code i have select specific column value and assign it to textbox. May this solve your problem. Remember i have two cells rows thats why i kept cell[1] because index start from 0.
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (i == 5)
{
textBox3.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
}
}

Add and find Dropdownlist at Runtime inside GridView

In my asp.net application, I have used Gridview control, In which i have to add Dropdownlist at runtime for each cell.Which i am able to bind successfully.
Below is my code which inside row databound event,
foreach (GridViewRow row in gdvLocation.Rows) {
if (row.RowType == DataControlRowType.DataRow) {
for (int i = 1; i < row.Cells.Count; i++) {
var dlRouteType = new DropDownList();
dlRouteType.ID = "ddlRouteType";
dlRouteType.DataSource = GetRouteTypeList();
dlRouteType.DataTextField = "RouteType";
dlRouteType.DataValueField = "Id";
dlRouteType.DataBind();
row.Cells[i].Controls.Add(dlRouteType);
}
}
}
I have a button in my page, which has functionality to save data to database . While saving data i have to pass the value from Dropdownlist which i have added at runtime. On button click i am writing following code to get data from dropdownlist,
var ddlDropDown = (DropDownList)row.Cells[i].FindControl("ddlRouteType");
But i am getting null in ddlDropDown object. I have even added Update panel inside aspx page. Any suggessions most welcome.
Thanks in advance
Sangeetha
You have these errors in your code
RowDataBound already iterates through each rows and so all you need not write that foreach on top
You are iterating from index 1, index are zero-based. So start from zero.
The DropDownList ID must be unique, so better write something like,dlRouteType.ID = "ddlRouteType_" + i;
The code should be,
protected void gdvLocation_RowDataBound(object sender, GridViewRowEventArgs e)
{
//removed the foreach loop
var row = e.Row;
if (row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < row.Cells.Count; i++) //changed index
{
var dlRouteType = new DropDownList();
dlRouteType.ID = "ddlRouteType_" + i; //gave unique id
dlRouteType.DataSource = GetRouteTypeList();
dlRouteType.DataTextField = "RouteType";
dlRouteType.DataValueField = "Id";
dlRouteType.DataBind();
row.Cells[i].Controls.Add(dlRouteType);
}
}
}

How to make the WPF Datagrid editable while creating dynamically, without giving any Itemsource

I am new to WPF, I am creating a datagrid dynamically by taking the NoOfRows & NoOfColumns as input. But the datagrid is not becoming Editable as I am not passing any Item source.
Is There any way of making the datagrid editable while not giving any Item source?
My Code is Like this (I have taken checkbox columns as datagrid column, I want to check the checkboxes):
var row = Convert.ToInt32(TbxLevels.Text);
var column = Convert.ToInt32(TbxPositions.Text);
Dgrid.Columns.Clear();
Dgrid.Items.Clear();
for (int i = 0; i < column; i++)
{
var c = new DataGridCheckBoxColumn();
c.Header = column;
Dgrid.Columns.Add(c);
}
for (int i = 0; i < row; i++)
{
Dgrid.Items.Add(i);
}

Datagridivew DataGridViewComboBoxColumn select value member

i have datagridview which has one combobox column. i populate combobox column. when i select any text from combobox column then i need to get the value when i read the data in for loop.
dgFilter is here datagridview
DataGridViewComboBoxColumn dgcoSpec = new DataGridViewComboBoxColumn();
dgcoSpec = new DataGridViewComboBoxColumn();
dgcoSpec.DataSource = loadOperators();
dgcoSpec.DisplayMember = "Operatortxt";
dgcoSpec.ValueMember = "Operatorvalue";
dgcoSpec.AutoComplete = true;
dgcoSpec.HeaderText = "Operators";
dgcoSpec.Name = "Operators";
dgcoSpec.DefaultCellStyle.NullValue = "--Select--";
dgcoSpec.Width = 130;
dgFilter.Columns.Insert(1, dgcoSpec);
here this way i read data from combobox column
for (int i = 0; i <= dgFilter.Rows.Count - 1; i++)
{
strOperator = dgFilter.Rows[i].Cells[1].Value.ToString().Trim();
}
but the problem is i am not getting the combox value member rather i am getting display member.
so how to extract value member from for loop. please guide me with code. thanks
The value member is the string that is displayed inside the DataGridViewComboboxCell.
The actual Combobox control only exists during the time frame in which the user is editing the cell.
If you mean that you would like to get the index of the value in the list of the DataGridViewComboboxCell items, you can query the index of the value:
for (int i = 0; i <= dgFilter.Rows.Count - 1; i++)
{
var cell = dgFilter.Rows[i].Cells[1] as DataGridViewComboboxCell;
int index = cell == null || cell.Value == null ? -1 : cell.Items.IndexOf(cell.Value);
Console.WriteLine(index);
}
In this example, I am using -1 as an invalid value.
EDIT Just noticed you are using a DataSource;
See DataGridViewComboBoxColumn name/value how? for possible duplicate.

Categories

Resources