{"id":74,"date":"2009-10-08T22:08:29","date_gmt":"2009-10-08T21:08:29","guid":{"rendered":"http:\/\/blog.rabihtawil.com\/?p=48"},"modified":"2009-10-08T22:08:29","modified_gmt":"2009-10-08T21:08:29","slug":"managing-bindingsource-position-in-data-driven-winforms","status":"publish","type":"post","link":"http:\/\/www.raytawil.com\/?p=74","title":{"rendered":"Managing BindingSource Position in Data Driven WinForms"},"content":{"rendered":"<p><span>If you are working with a <span class=\"IL_LINK_STYLE\" style=\"border-bottom: 1px solid #ff0000; text-decoration: underline; color: #ff0000; font-size: 12.1667px; font-weight: 400; font-style: normal; font-family: verdana,arial,sans-serif;\">Windows Form<\/span><span> where a <span class=\"IL_SPAN\"><input name=\"IL_MARKER\" type=\"hidden\" \/>user<\/span><span> can create and <span class=\"IL_SPAN\"><input name=\"IL_MARKER\" type=\"hidden\" \/>edit<\/span> data in the same form you may run into the problem of trying to set the BindingSource to\u00a0a given record in a DataSet you want to work with. <\/span><\/span><\/span><\/p>\n<p><span><span>For example let&#8217;s say you have a <span class=\"IL_LINK_STYLE\" style=\"border-bottom: 1px solid #ff0000; text-decoration: underline; color: #ff0000; font-size: 12.1667px; font-weight: 400; font-style: normal; font-family: verdana,arial,sans-serif;\">DataGridView<\/span><span> in a <span class=\"IL_LINK_STYLE\" style=\"border-bottom: 1px solid #ff0000; text-decoration: underline; color: #ff0000; font-size: 12.1667px; font-weight: 400; font-style: normal; font-family: verdana,arial,sans-serif;\">Smart Client<\/span><span>.\u00a0 When the\u00a0<span class=\"IL_SPAN\"><input name=\"IL_MARKER\" type=\"hidden\" \/>user<\/span> double clicks a given row in the <\/span><\/span><span class=\"IL_SPAN\"><input name=\"IL_MARKER\" type=\"hidden\" \/>DataGridView<\/span><span> a\u00a0form opens so they can <span class=\"IL_SPAN\"><input name=\"IL_MARKER\" type=\"hidden\" \/>edit<\/span> the data.\u00a0 More times than not (no matter how many demos you watch) user&#8217;s do not <span class=\"IL_SPAN\"><input name=\"IL_MARKER\" type=\"hidden\" \/>edit<\/span> data values in DataGridViews.\u00a0 Although this is a supported feature, <\/span><\/span><span class=\"IL_LINK_STYLE\" style=\"border-bottom: 1px solid #ff0000; text-decoration: underline; color: #ff0000; font-size: 12.1667px; font-weight: 400; font-style: normal; font-family: verdana,arial,sans-serif;\">business objects<\/span><span><span><span> are too complex.\u00a0 Imagine having to <span class=\"IL_SPAN\"><input name=\"IL_MARKER\" type=\"hidden\" \/>edit<\/span><span> a contact in a <span class=\"IL_LINK_STYLE\" style=\"border-bottom: 1px solid #ff0000; text-decoration: underline; color: #ff0000; font-size: 12.1667px; font-weight: 400; font-style: normal; font-family: verdana,arial,sans-serif;\">single row<\/span> in Outlook for example,\u00a0not fun and not very <\/span><\/span><span class=\"IL_SPAN\"><input name=\"IL_MARKER\" type=\"hidden\" \/>user<\/span> friendly.\u00a0\u00a0Why every <\/span><span class=\"IL_LINK_STYLE\" style=\"border-bottom: 1px solid #ff0000; text-decoration: underline; color: #ff0000; font-size: 12.1667px; font-weight: 400; font-style: normal; font-family: verdana,arial,sans-serif;\">Microsoft demo<\/span> I watch shows editing in DataGridViews is beyond me.\u00a0 I digress though.<\/span><\/span><\/p>\n<p><span><span><span><span>When the new form is instantiated we need to pass two things:\u00a0 the dataset we are working with, and the IDof the item we want to <span class=\"IL_LINK_STYLE\" style=\"border-bottom: 1px solid #ff0000; text-decoration: underline; color: #ff0000; font-size: 12.1667px; font-weight: 400; font-style: normal; font-family: verdana,arial,sans-serif;\">edit<\/span>.\u00a0 We can pass this information into the constructor of the form without any trouble as seen below. By passing in the DataSet\u00a0we can add new records, delete records or whatever\u00a0we need to do.\u00a0 The other benefit is since the dataset is passed by reference, any changes to\u00a0it will automatically appear in the row the <\/span><span class=\"IL_LINK_STYLE\" style=\"border-bottom: 1px solid #ff0000; text-decoration: underline; color: #ff0000; font-size: 12.1667px; font-weight: 400; font-style: normal; font-family: verdana,arial,sans-serif;\">user<\/span> just double clicked on.\u00a0 In order for the form fields to be auto bound to the record the <span class=\"IL_SPAN\"><input name=\"IL_MARKER\" type=\"hidden\" \/>user<\/span> clicked on\u00a0we need to set the DataBinding of the controls\u00a0in the form to a\u00a0BindingSource.\u00a0 The BindingSource serves as <\/span><span class=\"IL_LINK_STYLE\" style=\"border-bottom: 1px solid #ff0000; text-decoration: underline; color: #ff0000; font-size: 12.1667px; font-weight: 400; font-style: normal; font-family: verdana,arial,sans-serif;\">the go between<\/span> between the UI and data store.\u00a0\u00a0\u00a0 Here&#8217;s a sample &#8220;CellDoubleClick&#8221; event that would happen in the <\/span><span class=\"IL_SPAN\"><input name=\"IL_MARKER\" type=\"hidden\" \/>DataGridView<\/span>:<\/span><\/p>\n<div class=\"cf\">\n<p class=\"cl\"><span class=\"cln\"><span style=\"color: #2b91af;\"> 1<\/span><\/span> <span class=\"cb1\"><span style=\"color: #0000ff;\">int<\/span><\/span> id = <span class=\"cb2\"><span style=\"color: #008080;\">Convert<\/span><\/span>.ToInt32(<span class=\"cb2\"><span style=\"color: #008080;\"><span><span class=\"IL_SPAN\"><input name=\"IL_MARKER\" type=\"hidden\" \/>DataGridView<\/span><\/span><\/span><\/span>.Rows[e.RowIndex].Cells[0].Value);<\/p>\n<p class=\"cl\"><span class=\"cln\"><span style=\"color: #2b91af;\"> 2<\/span><\/span> MyForm myForm = <span class=\"cb1\"><span style=\"color: #0000ff;\">new<\/span><\/span> MyForm(dataSet, id);<\/p>\n<p class=\"cl\"><span class=\"cln\"><span style=\"color: #2b91af;\"> 3<\/span><\/span> myForm.Show();<\/p>\n<\/div>\n<p><!--EndFragment-->As the form loads the problem we face is\u00a0having to tell the BindingSource in the form which record we want to set as the <a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/msdn2.microsoft.com\/en-us\/library\/system.windows.forms.bindingsource.current.aspx');\"  href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/system.windows.forms.bindingsource.current.aspx\">BindingSource.Current<\/a><span> item.\u00a0 At first glance it looks like we would just get the DataRow and then set it as the Current <span class=\"IL_LINK_STYLE\" style=\"border-bottom: 1px solid #ff0000; text-decoration: underline; color: #ff0000; font-size: 12.1667px; font-weight: 400; font-style: normal; font-family: verdana,arial,sans-serif;\">property<\/span>.\u00a0 Wrong.\u00a0 The BindingSource.Current <span class=\"IL_SPAN\"><input name=\"IL_MARKER\" type=\"hidden\" \/>property<\/span> has only\u00a0a get accessor.\u00a0 Instead we need to use the <\/span><a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/msdn2.microsoft.com\/en-us\/library\/system.windows.forms.bindingsource.position.aspx');\"  href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/system.windows.forms.bindingsource.position.aspx\">BindingSource.Position<\/a><span> <span class=\"IL_SPAN\"><input name=\"IL_MARKER\" type=\"hidden\" \/>property<\/span>.\u00a0\u00a0Note:\u00a0 Any change to the position <span class=\"IL_SPAN\"><input name=\"IL_MARKER\" type=\"hidden\" \/>property<\/span> updates the current <span class=\"IL_SPAN\"><input name=\"IL_MARKER\" type=\"hidden\" \/>property<\/span> used by the bindingsource.\u00a0\u00a0The problem we face now\u00a0is the\u00a0BindingSource.Position <span class=\"IL_SPAN\"><input name=\"IL_MARKER\" type=\"hidden\" \/>property<\/span> wants to know the <\/span><em>index<\/em> of the item in the underlying DataSource.<\/p>\n<p><span>We don&#8217;t know <span class=\"IL_SPAN\"><input name=\"IL_MARKER\" type=\"hidden\" \/>the index<\/span> value used by the BindingSource\u00a0but we do know the ID we want to use.\u00a0 This is where the <\/span><a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/msdn2.microsoft.com\/en-us\/library\/ms158165.aspx');\"  href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/ms158165.aspx\">BindingSource.Find()<\/a><span> method comes in.\u00a0 Using the find method we can basically search the underlying datasource and retrieve <span class=\"IL_SPAN\"><input name=\"IL_MARKER\" type=\"hidden\" \/>the index<\/span> of a given item.\u00a0 Here&#8217;s an example:<\/span><\/p>\n<p><span style=\"font-size: x-small;\"> <\/span><span style=\"color: #ff0000; font-size: small;\">contactBindingSource.Position = contactBindingSource.Find(&#8220;Id&#8221;, 2);<\/span><\/p>\n<p><span>Once the line above runs, it is going to search the column &#8220;Id&#8221; in the DataSource associated with the BindingSource and return <span class=\"IL_SPAN\"><input name=\"IL_MARKER\" type=\"hidden\" \/>the index<\/span> of that item. <\/span><\/p>\n<p>What if you use the same form to create a new item?\u00a0 Simple, call the <a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/msdn2.microsoft.com\/en-us\/library\/system.windows.forms.bindingsource.addnew.aspx');\"  href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/system.windows.forms.bindingsource.addnew.aspx\">BindingSource.AddNew()<\/a><span> method which will add a new item to the underlying DataSource.\u00a0 Then move <span class=\"IL_SPAN\"><input name=\"IL_MARKER\" type=\"hidden\" \/>the index<\/span> to the last item.\u00a0 Here&#8217;s a sample:<\/span><\/p>\n<p><span style=\"font-size: x-small;\"><span style=\"font-size: small;\"> bindingSource.AddNew();<br \/>\n<\/span><span style=\"font-size: small;\"> bindingSource.MoveLast();<\/span><\/p>\n<p><span style=\"font-size: small;\"><span>Since items are always added at the end of the associated data source the MoveLast() method\u00a0will set <span class=\"IL_LINK_STYLE\" style=\"border-bottom: 1px solid #ff0000; text-decoration: underline; color: #ff0000; font-size: 16px; font-weight: 400; font-style: normal; font-family: verdana,arial,sans-serif;\">the index<\/span> to the last item in the underlying datasource.<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\">I hope this makes sense because it is an easy way to have a form bound to a DataSet and use the same form to perform &#8220;CRUD&#8221; operations with.<\/span><\/p>\n<p><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are working with a Windows Form where a user can create and edit data in the same form you may run into the problem of trying to set the BindingSource to\u00a0a given record in a DataSet you want to work with. For example let&#8217;s say you have a DataGridView in a Smart Client.\u00a0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[110],"tags":[15,114,115,32,34,97],"_links":{"self":[{"href":"http:\/\/www.raytawil.com\/index.php?rest_route=\/wp\/v2\/posts\/74"}],"collection":[{"href":"http:\/\/www.raytawil.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.raytawil.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.raytawil.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.raytawil.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=74"}],"version-history":[{"count":0,"href":"http:\/\/www.raytawil.com\/index.php?rest_route=\/wp\/v2\/posts\/74\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.raytawil.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=74"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.raytawil.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=74"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.raytawil.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=74"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}