[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 719
  • Last Modified:

BindingContext in .Net

Hello Experts,

In my C# application, I have a form with 10 textboxes and a datatable with 10 rows. I need to bind all textboxes to the same column in the datatable but different rows. Is this posible? Please help.

Thanks,
Harish
0
Harish Varghese
Asked:
Harish Varghese
  • 5
  • 5
1 Solution
 
princeatapiCommented:
i believe you know all the id's of text box just rotate like this

DataTableObj.Rows[RwoIndex]["ColumnName"] = TextBox.text;
0
 
princeatapiCommented:
<asp:Panel ID="PanelTest" runat="server">
            <asp:TextBox ID="TextBox1" runat="server" Text ="One"></asp:TextBox>
            <asp:TextBox ID="TextBox5" runat="server" Text ="two"></asp:TextBox>
            <asp:TextBox ID="TextBox4" runat="server" Text ="One"></asp:TextBox>
            <asp:TextBox ID="TextBox3" runat="server" Text ="One"></asp:TextBox>
            <asp:TextBox ID="TextBox2" runat="server" Text ="One"></asp:TextBox>
        </asp:Panel>


        for (int i = 1; i <= 5; i++)
        {
            string Res = ((TextBox)PanelTest.FindControl("TextBox" + i.ToString())).Text;
            Response.Write(Res);
        }

use the technique given and insert into the column of datatable
0
 
Harish VargheseProject LeaderAuthor Commented:
Hi princeatapi,

This is my code:

txtValue1.DataBindings.Add("Text", datatable, "paramvalue");
txtValue2.DataBindings.Add("Text", datatable, "paramvalue");

I added below code after above code:
datatable.Rows[0]["paramvalue"] = txtValue1.Text;
datatable.Rows[1]["paramvalue"] = txtValue2.Text;

But that did not make any difference. If I enter value 'value1' for txtValue1 and 'value2' for txtValue2,
the last entered value (value2) only is getting saved.

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Harish VargheseProject LeaderAuthor Commented:
I want to bind the control (textboxes) directly.
0
 
joriszwaenepoelCommented:
Is it WPF?  What technology are you using?
0
 
princeatapiCommented:
you must add this line at the end to ensure the changes in DataTable

datatable.AcceptChanges();
0
 
princeatapiCommented:
are you sure you have that many number of rows ??
0
 
princeatapiCommented:
k just try like this

DataRow temp = datatable.NewRow();
temp["paramvalue"] = textbox1.text
.
./* repeat steps for 4 more textBox */
.

datatable.Rows.Add(temp);
datatable.AcceptChanges();

finally your dataTable will have 5 rows :)
0
 
Harish VargheseProject LeaderAuthor Commented:
Hi joriszwaenepoel,

I am using Windows Forms Application.

princeatapi,

I think your solution copies the values from unbound textboxes to datatable before updating database.
I need these textboxes to be bound to the datatable and the values in textboxes should reflect automatically in corresponding rows in Data Table.
0
 
Harish VargheseProject LeaderAuthor Commented:
I was able to bind different textboxes to different rows as below:

//Bind txtValue1 to row 0 of data table
txtValue1.DataBindings.Add("Text", datatable, "ParamValue");
txtValue1.BindingContext = new BindingContext();
CurrencyManager cm = (CurrencyManager)txtValue1.BindingContext[datatable];
cm.Position = 0;

//Bind txtValue2 to row 1 of data table
txtValue2.DataBindings.Add("Text", datatable, "ParamValue");
txtValue2.BindingContext = new BindingContext();
CurrencyManager cm = (CurrencyManager)txtValue1.BindingContext[datatable];
cm.Position = 1;

Now I have a different problem:
When I change values in textboxes, the values get reflected in DataTable, but the RowState of these rows remains "Unchanged". Why are they not showing "Modified"? How can I get them as "Modified"?
 
0
 
Harish VargheseProject LeaderAuthor Commented:
Ok. I got it solved by adding an event handler for ColumnChanged event of Data Table:

private void datatable_ColumnChanged(object sender, DataColumnChangeEventArgs e)
{
       e.Row.EndEdit();
}

Now the row state becomes "Modified"
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now